Python使用变量和if语句

时间:2013-10-29 01:38:24

标签: python variables if-statement statements

我正在完成一项任务,我必须进行测验。到目前为止,这是我的代码。

print("Hello and welcome to Shahaad's quiz!") #Introduction
name = input("What is your name? ")
print("Alright", name,", these will be today's topics:")  #Topics
print("a) Video Games") 
print("b) Soccer")
print("c) Geography") 
choice = input("Which topic would you like to begin with?")
if choice == 'video games' or choice == 'Video Games' or choice == 'Video games' or choice == 'a)' or choice == 'a':
    print("You picked Video Games.")
print("Question number one:")                                      #Question one
print("What is the most popular FPS (First Person Shooter) game?")
print("a) Call of Duty")
print("b) Battlefield")
print("c) Grand Theft Auto 5")
print("d) Counter Strike")       
maxGuesses = 2 #Max number of attempts for the problem
guessesTaken = 0
points = 0
question = input("Your answer: ")
if question == 'Call of duty' or question == 'call of duty' or question == 'Call Of Duty' or question == 'Call of Duty' or question == 'a' or question == 'a)':
    print("You are correct! You guessed the question on the first try!")
    points = points + maxGuesses-guessesTaken
    print("You scored",(maxGuesses-guessesTaken), "points!")
else:
    print("Incorrect!")
    print("You have", (maxGuesses-guessesTaken-1), "guesses remaining!")
    answerb = input("Your answer: ")
if answerb == 'Call of duty' or answerb == 'call of duty' or answerb == 'Call Of Duty' or answerb == 'Call of Duty' or answerb == 'a' or answerb == 'a)':
    print("You are correct!")
    points = points + maxGuesses-guessesTaken
    print("You scored", (maxGuesses-guessesTaken-1), "points!")

我遇到的问题是在第28行,它说answerb没有定义,但我确实定义了它。我应该用我学过的术语进行这个测验,我不允许使用我没有学过的术语。我把else:打印错误并输入answerb ==输入给用户第二次回答的机会。如果他们第一次尝试,他们不需要为answerb插入一些东西。

3 个答案:

答案 0 :(得分:1)

这一行...

    answerb = input("Your answer: ")

缩进太远了。它只会在前面的if语句的“else”部分中调用。

answerb = input("Your answer: ")

取消它,使其与下列if处于同一级别,以便始终调用它(因此将始终定义answerb)。

答案 1 :(得分:0)

修改

根据您的评论以及您的代码结构,我认为您希望缩进这一部分:

if answerb == 'Call of duty' or answerb == 'call of duty' or answerb == 'Call Of Duty' or answerb == 'Call of Duty' or answerb == 'a' or answerb == 'a)':
    print("You are correct!")
    points = points + maxGuesses-guessesTaken

您的代码应如下所示:

print("Hello and welcome to Shahaad's quiz!") #Introduction
name = input("What is your name? ")
print("Alright", name,", these will be today's topics:")  #Topics
print("a) Video Games") 
print("b) Soccer")
print("c) Geography") 
choice = input("Which topic would you like to begin with?")
if choice == 'video games' or choice == 'Video Games' or choice == 'Video games' or choice == 'a)' or choice == 'a':
    print("You picked Video Games.")
print("Question number one:")                                      #Question one
print("What is the most popular FPS (First Person Shooter) game?")
print("a) Call of Duty")
print("b) Battlefield")
print("c) Grand Theft Auto 5")
print("d) Counter Strike")       
maxGuesses = 2 #Max number of attempts for the problem
guessesTaken = 0
points = 0
question = input("Your answer: ")
if question == 'Call of duty' or question == 'call of duty' or question == 'Call Of Duty' or question == 'Call of Duty' or question == 'a' or question == 'a)':
    print("You are correct! You guessed the question on the first try!")
    points = points + maxGuesses-guessesTaken
    print("You scored",(maxGuesses-guessesTaken), "points!")
else:
    print("Incorrect!")
    print("You have", (maxGuesses-guessesTaken-1), "guesses remaining!")
    answerb = input("Your answer: ")
    if answerb == 'Call of duty' or answerb == 'call of duty' or answerb == 'Call Of Duty' or answerb == 'Call of Duty' or answerb == 'a' or answerb == 'a)':
        print("You are correct!")
        points = points + maxGuesses-guessesTaken
    print("You scored", (maxGuesses-guessesTaken-1), "points!")

答案 2 :(得分:-1)

  1. 不,您尚未正确定义answerb。它在else语句中,可能尚未执行。我假设你想要的是缩进你的最后if语句,因为只有在答案错误时才会执行。

  2. 而不是多个question ==使用question in ['bla1', 'bla2', 'bla3']。在这里你甚至不需要这个,因为你不关心大写。您可以执行question.lower() == 'bla bla bla',它将匹配所有可能的大写字母。 (与其他if相同)

  3. 你的程序结构太可怕了。您需要使用循环重写它并将您的问题和答案存储在数组中。