使用def函数重新运行Python程序

时间:2019-03-23 22:08:48

标签: python

这个想法是程序将被用来具有许多功能。根据用户输入的内容,它们将被发送到程序中的某个特定def,并可以选择在末尾再次保存数据。

score = 0

def Quiz():
    print ("Welcome to the quiz.")
    name = input("Please type your name:")

    print ("Answer the question.")
    Answer == input ("Please enter the answer to 5+6.")

Quiz()

if Answer == "11":
 score = score + 1
 print("Your answer is correct and your score is", score) 
else:
 print ("Your answer is wrong. Your score is", score)

again = str(input("Do you want to play again (type yes or no): "))
if again == "yes":
    Quiz()
else:
    sys.exit(0)

我只是不确定为什么它不会运行该程序,然后如果用户输入是,则会循环返回。

任何帮助,我想我都快结束了。

4 个答案:

答案 0 :(得分:3)

代码有些错误。 6502在另一个答案中提到了其中一个:通过拥有第二个Quiz(),程序将在运行Quiz函数后返回到该点,因此它将在第二次执行后自动退出。

其他一些事情:

  • Answer == input (' ')失败,因为==使Python认为它正在评估等效性,而不是将输入分配给Answer
  • 答案不是全局变量,也不传递回主程序,因此当您尝试检查其值时它将失败

从第一句话开始,我不确定您要对过程进行什么操作,因此需要将Answer定义为全局,或者需要将其从函数中传回,如下所示:

score = 0

def Quiz():
    print ("Welcome to the quiz.")
    name = input("Please type your name:")

    print ("Answer the question.")
    Answer = input ("Please enter the answer to 5+6.")
    return Answer

while True:

    Answer = Quiz()

    if Answer == "11":
     score = score + 1
     print("Your answer is correct and your score is", score)
    else:
     print ("Your answer is wrong. Your score is", score)

    again = str(input("Do you want to play again (type yes or no): "))
    if again == "no":
        break

但是整个过程可以更简单地完成,而无需执行以下程序:

score = 0

while True:
    print ("Welcome to the quiz.")
    name = input("Please type your name:")

    print ("Answer the question.")
    Answer = input ("Please enter the answer to 5+6.")

    if Answer == "11":
     score = score + 1
     print("Your answer is correct and your score is", score)
    else:
     print ("Your answer is wrong. Your score is", score)

    again = str(input("Do you want to play again (type yes or no): "))
    if again == "no":
        break

最后,QuizAnswer都不应真正以大写字母开头。根据PEP8标准https://realpython.com/python-pep8/#naming-styles,函数和变量名应以小写字母开头。这看似微不足道,但是即使在Stack Overflow问题中,您也可以看到Answer具有与函数名称相同的颜色,并且不同于其他变量,因此以后可能会引起混淆。

答案 1 :(得分:1)

有一些错误。除了先前的答案,这也不对:

Answer == input ("Please enter the answer to 5+6.")

程序将因此失败。您想将输入分配给变量Answer,但是==用于比较。 然后:

if Answer == "11":

此处未定义变量Answer。您只能在功能测验中定义它。

关于您的问题,您可以使用while循环:

from random import randint


def quiz():    
    print ("Answer the question.")
    a, b = randint(0, 9), randint(0, 9)
    answer = input("Please enter the answer to {0} + {1}: ".format(a, b))
    return a + b == int(answer)

if __name__=='__main__':          
    again = 'yes'
    score = 0
    print ("Welcome to the quiz.")
    name = input("Please type your name: ")

    while again == 'yes':  # it will go inside at least once         
        if quiz():
            score += 1
            print("Your answer is correct and your score is:", score) 
        else:
            print ("Your answer is wrong. Your score is", score)
        again = str(input("Do you want to play again (type yes or no): "))

    print('Your total score is:', score)

答案 2 :(得分:0)

score = 0

print ("Welcome to the quiz.")
name = input("Please type your name:")

while True:    
    print ("Answer the question.")
    Answer = input ("Please enter the answer to 5+6.") # only one '='!

    if Answer == "11":
        score = score + 1
        print("Your answer is correct and your score is", score) 
    else:
        print ("Your answer is wrong. Your score is", score)

    again = str(input("Do you want to play again (type yes or no): "))
    if again == "no":
        break # this exits the loop

答案 3 :(得分:-1)

在您问完问题答案后第二次调用Quiz()时,程序将继续执行并到达末尾,从而退出。

您要执行的是while循环:

score = 0

print ("Welcome to the quiz.")
name = input("Please type your name:")

while True:    
    print ("Answer the question.")
    Answer = input ("Please enter the answer to 5+6.") # only one '='!

    if Answer == "11":
        score = score + 1
        print("Your answer is correct and your score is", score) 
    else:
        print ("Your answer is wrong. Your score is", score)

    again = str(input("Do you want to play again (type yes or no): "))
    if again == "no":
        break # this exits the loop