我的程序两次打印20个问题,而不是10个问题

时间:2019-03-21 06:46:46

标签: python loops extra

我的程序应该打印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!') 

2 个答案:

答案 0 :(得分:0)

尝试初始化问题轻松于

questionsEasy = {} 对于范围在(10)中的我:

答案 1 :(得分:0)

您应该在questionsEasy函数中定义easy。您的程序第二遍打印20个问题的原因是questionsEasy是在easy函数之外定义的,并且保留其先前的值,并且每次将再添加10个问题


如果您不熟悉编程,则可以使用http://www.pythontutor.com/live.html#mode=edit

可视化您的程序