簡體   English   中英

Python使用變量和if語句

[英]Python using variables and if 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尚未定義,但我確實定義了它。 我應該使用我已經學過的術語來進行此測驗,並且不允許我使用諸如一段時間內未學到的術語。 我輸入其他內容:打印錯誤,並輸入answerb ==輸入,以使用戶有第二次回答的機會。 並且,如果他們第一次嘗試,就不需要為answerb插入內容。

這條線...

    answerb = input("Your answer: ")

縮進太遠。 它只會在前面的if語句的“ else”部分中調用。

answerb = input("Your answer: ")

取消縮進使其與以下if處於同一級別,以便始終對其進行調用(因此將始終定義answerb )。

編輯:

根據您的注釋以及代碼結構,我認為您希望縮進這一部分:

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!")
  1. 不,您沒有正確定義answerb 它位於else語句中,可能尚未執行。 我假設您要縮進最后一個if語句,因為僅當答案錯誤時才執行該語句。

  2. 代替多個question == question in ['bla1', 'bla2', 'bla3']使用question in ['bla1', 'bla2', 'bla3'] 在這里,您甚至不需要這個,因為您不必擔心大小寫。 您可以執行question.lower() == 'bla bla bla' ,它將匹配所有可能的大寫字母。 (同樣用於其它if “S)

  3. 您的程序的結構是可怕的。 您需要使用循環將其重寫並將問題和答案存儲在數組中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM