在我的Python游戏和骰子滚动游戏中,如果您猜对了数字,我会在其中工作,但是我正在尝试开发更高或更低的部分
但是在我的代码中,如果该值为3,并且您说得更高,它将表示您已经赢了。
示例#debug (5)
Would you like to bet on higher or lower <h:l> <: l
You won congrats!
但是我应该在这里输了而不是赢。
elif user_nh == 'high':
print(system_number)
hiLo = str(input("Would you like to bet on higher or lower <h:l> <: "))
if hiLo == 'l' and system_number == 1 or system_number == 2 or system_number == 3:
print("You won congrats")
pa = str(input("Would you like to play again y/n :> "))
if pa == 'n':
break
if pa == 'y':
continue
else:
print("You supplied a invalid response, quitting game.")
break
if hiLo == 'h' and system_number == 4 or system_number == 5 or system_number == 6:
print('You won congrats!')
pa = str(input("Would you like to play again y/n :> "))
if pa == 'n':
break
if pa == 'y':
continue
else:
print("You supplied a invalid response, quitting game.")
break
答案 0 :(得分:0)
更好的是,由于您已经定义了较高和较低的数字,因此最好使用它们,如下所示:
第39行:
if hiLo == 'l' and system_number in lower:
第49行:
if hiLo == 'h' and system_number in higher:
答案 1 :(得分:-1)
像这样放入括号:
instance
但是您最好这样做:
if (hiLo == 'l') and (system_number == 1 or system_number == 2 or system_number == 3)
答案 2 :(得分:-1)
此行:
if hiLo == 'l' and system_number == 1 or system_number == 2 or system_number == 3:
正在评估类似这样的内容:
if (hiLo == 'l' and system_number == 1): # Enter block
elif system_number == 2: # Enter block
elif system_number == 3: # Enter block
else: # Don't enter block
因此hiLo == 'l'
部分(当前)仅影响您的system_number == 1
测试中的第一个。如果hiLo
是2或3,则无论system_number
的值如何,代码都会进入该块。
这是因为or
运算符比and
运算符具有higher precedence。
所以您可能想要类似的东西:
if hiLo == 'l' and (system_number == 1 or system_number == 2 or system_number == 3):
或
if hiLo == 'l' and system_number in [1,2,3]:
或
if hiLo == 'l' and (1 <= and system_number <= 3):