希望使用edge cas获得50%的成功

2024-04-25 00:11:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小游戏,我想50产生一个胜利,但仍然保持代码是如何使用边缘的情况下

import random

roll = input('Press Enter to Spin the Wheel or Type Stop!')

while roll == "":
    prize = random.randint(0, 100)
    print ('Youre number is ', prize )
    if prize < 50: 
     print ('Sorry, you didnt win. Try again')
    if prize > 50: 
     print ('Congratulations! Youre a winner!')

    roll = input('Press Enter to Spin the Wheel or type Stop!')

if roll == 'Stop' or roll=='stop': 
    print ('Thank you for playing')
else:
    print ('Well, so long!')

谢谢你的帮助


Tags: orthetoinputifrandomwheelpress
2条回答

您应该检查prize是否等于50:

if prize == 50:
    print ('Congratulations! Youre a winner!')
else:
    print ('Sorry, you didnt win. Try again')

您的条件正在检查严格大于或严格小于50的数字。您需要将其中一个条件更改为不太严格,同时允许50计数:

if prize < 50: 
 print ('Sorry, you didnt win. Try again')
if prize >= 50:                 # use >= to test if prize is greater than *or equal* to fifty
 print ('Congratulations! Youre a winner!')

相关问题 更多 >