程序不会根据输入调用正确的函数。不知道我错过了什么
Program won't call correct function based on input. No idea what I'm missing
编辑:在 main() 中玩了一会儿之后,似乎程序正在选择 if/elif 块中调用的函数,而不管输入如何。我不知道为什么它在工作正常后开始这样做
我一遍又一遍地检查我的代码,但我无法弄清楚我没有看到什么。
这是一个数字序列游戏,它要求用户在简单和困难之间选择一个难度。它在某一时刻工作得很好,但现在无论选择什么,它每次都会进入简单模式。即使您根本没有任何输入就按下回车键。
#main program function and difficulty selection
def main():
print('-----------------------------------------------')
print('Please choose a difficulty.')
difficulty = str(input('(e)asy|(h)ard: '))
print('-----------------------------------------------')
if difficulty == 'easy'or'e':
easy()
elif difficulty == 'hard'or'h':
hard()
然后我有一个简单的函数和一个困难的函数。
Hard 只是 easy 函数,只改变生成的序列的大小,没有别的。我检查了每个块,没有任何会影响调用哪个函数的更改。
无论游戏玩了多少次都会发生,所以一定是我的 main() 函数有问题
如果有帮助的话,剩下的代码就在这里,也许我遗漏了一些明显的东西。
import random
def easy():
print ('Easy Mode','\n')
#Generates inital number, step value, and upper limit
num_sequence = 5
numbers = random.randint(1,101)
step = random.randint(1,20)
#Accumulates and prints all but last number in sequence
for num_generated in range (1, num_sequence):
print(numbers)
numbers = numbers + step
#Sets tries allowed and subtracts wrong attempts
guesses = 3
while guesses > 0:
user_num = int(input('Next Number: '))
guesses = guesses - 1
if user_num != numbers:
if guesses == 0:
break
else:
print('Try Again (Attempts Remaining:', guesses,')')
if user_num == numbers:
break
#Prints appropriate message based on game results
if user_num == numbers:
print ('Correct','\n')
if user_num != numbers:
print ('Attempts Exceeded: The answer was',numbers,'\n')
#block for hard difficulty (same as above, sequence size changed to 4)
def hard():
print ('Hard Mode','\n')
num_sequence = 4
#main program function and difficulty selection
def main():
print('-----------------------------------------------')
print('Please choose a difficulty.')
difficulty = str(input('(e)asy|(h)ard: '))
print('-----------------------------------------------')
if difficulty == 'easy'or'e':
easy()
elif difficulty == 'hard'or'h':
hard()
#block for replay selection
replay = 'y'
while replay == 'y':
main()
replay = input('Play again? (y)|(n): ',)
print ('\n')
if replay == 'n':
print('-----------------------------------------------')
print('Goodbye!')
print('-----------------------------------------------')
break
hard() 与前几个
之后逐行的 easy() 代码相同
当您进行复合比较(使用 or)时,条件的两边都必须完整。也就是说,
if difficulty == 'easy'or difficulty == 'e':
easy()
elif difficulty == 'hard'or difficulty == 'h':
hard()
否则你会说 "if difficulty == 'easy' >> which is false, then assign difficulty to 'e'" 这不是本意。
编辑:在 main() 中玩了一会儿之后,似乎程序正在选择 if/elif 块中调用的函数,而不管输入如何。我不知道为什么它在工作正常后开始这样做
我一遍又一遍地检查我的代码,但我无法弄清楚我没有看到什么。 这是一个数字序列游戏,它要求用户在简单和困难之间选择一个难度。它在某一时刻工作得很好,但现在无论选择什么,它每次都会进入简单模式。即使您根本没有任何输入就按下回车键。
#main program function and difficulty selection
def main():
print('-----------------------------------------------')
print('Please choose a difficulty.')
difficulty = str(input('(e)asy|(h)ard: '))
print('-----------------------------------------------')
if difficulty == 'easy'or'e':
easy()
elif difficulty == 'hard'or'h':
hard()
然后我有一个简单的函数和一个困难的函数。 Hard 只是 easy 函数,只改变生成的序列的大小,没有别的。我检查了每个块,没有任何会影响调用哪个函数的更改。
无论游戏玩了多少次都会发生,所以一定是我的 main() 函数有问题
如果有帮助的话,剩下的代码就在这里,也许我遗漏了一些明显的东西。
import random
def easy():
print ('Easy Mode','\n')
#Generates inital number, step value, and upper limit
num_sequence = 5
numbers = random.randint(1,101)
step = random.randint(1,20)
#Accumulates and prints all but last number in sequence
for num_generated in range (1, num_sequence):
print(numbers)
numbers = numbers + step
#Sets tries allowed and subtracts wrong attempts
guesses = 3
while guesses > 0:
user_num = int(input('Next Number: '))
guesses = guesses - 1
if user_num != numbers:
if guesses == 0:
break
else:
print('Try Again (Attempts Remaining:', guesses,')')
if user_num == numbers:
break
#Prints appropriate message based on game results
if user_num == numbers:
print ('Correct','\n')
if user_num != numbers:
print ('Attempts Exceeded: The answer was',numbers,'\n')
#block for hard difficulty (same as above, sequence size changed to 4)
def hard():
print ('Hard Mode','\n')
num_sequence = 4
#main program function and difficulty selection
def main():
print('-----------------------------------------------')
print('Please choose a difficulty.')
difficulty = str(input('(e)asy|(h)ard: '))
print('-----------------------------------------------')
if difficulty == 'easy'or'e':
easy()
elif difficulty == 'hard'or'h':
hard()
#block for replay selection
replay = 'y'
while replay == 'y':
main()
replay = input('Play again? (y)|(n): ',)
print ('\n')
if replay == 'n':
print('-----------------------------------------------')
print('Goodbye!')
print('-----------------------------------------------')
break
hard() 与前几个
之后逐行的 easy() 代码相同当您进行复合比较(使用 or)时,条件的两边都必须完整。也就是说,
if difficulty == 'easy'or difficulty == 'e':
easy()
elif difficulty == 'hard'or difficulty == 'h':
hard()
否则你会说 "if difficulty == 'easy' >> which is false, then assign difficulty to 'e'" 这不是本意。