我做了一个基本的随机程序对我的覆盆子皮,它有点像这样。
import random
print ("Welcome to the PC Expo's new game, PC Dispenser, what will you win?")
WinorLose = random.randint(1, 1000)
if WinorLose <100:
print ("You won a Nintendo Wii.")
elif WinorLose >200:
print ("You won a Sony PSP.")
elif WinorLose > 300:
print ("You won a Nintendo Wii U.")
elif WinorLose > 400:
print ("You won a Sony PS Vita.")
else:
print ("Not your lucky day, Try again.")
print ("Thank you for the visit.")
如果你不知道它是做什么的,它有机会给你一个虚拟的PSP,Wii U,等等但它所做的只是打印“你赢了一台索尼psp”,或者“不是你的幸运日,再试一次”。怎么了?有什么解决办法吗?
最佳答案
把最大的数字放在第一位:
WinorLose = random.randint(1, 1000)
print(WinorLose)
if WinorLose > 400:
print ("You won a Sony PS Vita.")
elif WinorLose > 300:
print ("You won a Nintendo Wii U.")
elif WinorLose > 200:
print ("You won a Sony PSP.")
elif WinorLose < 100:
print ("You won a Nintendo Wii.")
else:
print ("Not your lucky day, Try again.")
如果
WinorLose
是> 400
则它也是> 100
所以您将始终打印第一条语句。您可能还希望使用上界和下界:
if 400 <= WinorLose < 500:
print ("You won a Sony PS Vita.")
elif 300 <= WinorLose < 400:
print("You won a Nintendo Wii U.")
elif 200 <= WinorLose < 300:
print ("You won a Sony PSP.")
elif WinorLose < 200:
print ("You won a Nintendo Wii.")
else:
print ("Not your lucky day, Try again.")
使用
if 400 <= WinorLose < 500
等将按任意顺序工作,因为我们正在设置一个具有上下限的范围,因此除非WinorLose
在该范围内,否则语句的计算结果将不会为true。关于python - Python随机程序无法解决,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29222685/