当程序两次时,我的程序打印 20 个问题而不是 10 个
My program prints 20 questions instead of 10 when the program is twice
我的程序应该打印 10 个问题,当它完成后它会询问用户是否想再玩一次,如果他们按是,那么程序应该再次打印 10 个问题,但它打印了 20 个。我已尝试修复此问题,但无济于事。
score = 0
def mainmenu():
print('What difficulty would you like to play on?')
print('1. Easy')
print('2. Medium') #Three difficulties are shown from which the user can choose
print('3. Hard')
print('4. Quit')
selection = int(input('Choose a number: '))
if selection == 1:
choice = input('Are you sure you want to play on easy? [Y/N] ')
if choice.lower() == 'y' or choice.lower() == 'yes':
easy() #The user is asked to confirm if the difficulty they chose is the one they want
if choice.lower() == 'n' or choice.lower() == 'no':
print('Pick another difficulty') #If they say no they will be returned to the mainmenu using the define function
mainmenu()
if selection == 2:
choice2 = input('Are you sure you want to play on medium? [Y/N] ')
if choice2.lower() == 'y' or choice2.lower() == 'yes':
medium()
if choice2.lower() == 'n' or choice2.lower() == 'no':
print('Pick another difficulty')
mainmenu()
if selection == 3:
choice3 = input('Are you sure you want to play on hard? [Y/N] ')
if choice3.lower() == 'y' or choice3.lower() == 'yes':
hard()
if choice3.lower() == 'n' or choice3.lower() == 'no':
print('Pick another difficulty')
mainmenu()
if selection == 4:
exit()
def easy(): #Easy is defined to allow for smoother access from the mainmenu
global score
print('Heres come the questions!')
for i in range(10): #This process is repeated 10 times
int_a = random.randint(0,10)
int_b = random.randint(0,10) #Random numbers are generated through the use of the random module
operators = ['+'] #The operators are made into a key to allow for them to be chosen randomly
operators_value = random.choice(operators)
question = str(int_a) + ' ' + str(operators_value) + ' ' + str(int_b) #The two random numbers and the randomly chosen operators are made into a question
answerEasy = eval(question) #The answer to the question is then found using the eval function
question += ': '
questionsEasy.update({question:str(answerEasy)}) #The question and answer are put into a key to allow them to be matched up
for s in questionsEasy.keys():
useranswer = input(s)
if questionsEasy.get(s) == str(useranswer): #If the user enters the correct answer 1 point is added to the score
score += 1
print('You got it Correct!')
else:
print('You got it incorrect :(')
print('Nice ' + name + '!' + ' You got ' + str(score) + ' out of 10!')
尝试在 for
之前初始化 questionsEasy
问题简单={}
我在范围内(10):
您应该在 easy
函数中定义 questionsEasy
。你的程序第二次打印 20 个问题的原因是 questionsEasy
是在 easy 函数之外定义的,并且保留了它以前的值,你每次都增加了 10 个问题
如果您是编程新手,您可以使用 http://www.pythontutor.com/live.html#mode=edit
可视化您的程序
我的程序应该打印 10 个问题,当它完成后它会询问用户是否想再玩一次,如果他们按是,那么程序应该再次打印 10 个问题,但它打印了 20 个。我已尝试修复此问题,但无济于事。
score = 0
def mainmenu():
print('What difficulty would you like to play on?')
print('1. Easy')
print('2. Medium') #Three difficulties are shown from which the user can choose
print('3. Hard')
print('4. Quit')
selection = int(input('Choose a number: '))
if selection == 1:
choice = input('Are you sure you want to play on easy? [Y/N] ')
if choice.lower() == 'y' or choice.lower() == 'yes':
easy() #The user is asked to confirm if the difficulty they chose is the one they want
if choice.lower() == 'n' or choice.lower() == 'no':
print('Pick another difficulty') #If they say no they will be returned to the mainmenu using the define function
mainmenu()
if selection == 2:
choice2 = input('Are you sure you want to play on medium? [Y/N] ')
if choice2.lower() == 'y' or choice2.lower() == 'yes':
medium()
if choice2.lower() == 'n' or choice2.lower() == 'no':
print('Pick another difficulty')
mainmenu()
if selection == 3:
choice3 = input('Are you sure you want to play on hard? [Y/N] ')
if choice3.lower() == 'y' or choice3.lower() == 'yes':
hard()
if choice3.lower() == 'n' or choice3.lower() == 'no':
print('Pick another difficulty')
mainmenu()
if selection == 4:
exit()
def easy(): #Easy is defined to allow for smoother access from the mainmenu
global score
print('Heres come the questions!')
for i in range(10): #This process is repeated 10 times
int_a = random.randint(0,10)
int_b = random.randint(0,10) #Random numbers are generated through the use of the random module
operators = ['+'] #The operators are made into a key to allow for them to be chosen randomly
operators_value = random.choice(operators)
question = str(int_a) + ' ' + str(operators_value) + ' ' + str(int_b) #The two random numbers and the randomly chosen operators are made into a question
answerEasy = eval(question) #The answer to the question is then found using the eval function
question += ': '
questionsEasy.update({question:str(answerEasy)}) #The question and answer are put into a key to allow them to be matched up
for s in questionsEasy.keys():
useranswer = input(s)
if questionsEasy.get(s) == str(useranswer): #If the user enters the correct answer 1 point is added to the score
score += 1
print('You got it Correct!')
else:
print('You got it incorrect :(')
print('Nice ' + name + '!' + ' You got ' + str(score) + ' out of 10!')
尝试在 for
之前初始化 questionsEasy问题简单={} 我在范围内(10):
您应该在 easy
函数中定义 questionsEasy
。你的程序第二次打印 20 个问题的原因是 questionsEasy
是在 easy 函数之外定义的,并且保留了它以前的值,你每次都增加了 10 个问题
如果您是编程新手,您可以使用 http://www.pythontutor.com/live.html#mode=edit
可视化您的程序