0

私はクイズをしなければならない課題をやっています。これまでの私のコードは次のとおりです。

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: print wrong を入力し、 answerb == 入力を入力して、ユーザーに 2 回目の回答の機会を与えます。そして、彼らが最初の試行でそれを取得した場合、answerb に何かを挿入する必要はありません。

4

3 に答える 3

1

この線...

    answerb = input("Your answer: ")

インデントしすぎています。if前のステートメントの「else」部分でのみ呼び出されます。

answerb = input("Your answer: ")

次の と同じレベルになるようにインデントを解除して、if常に呼び出されるようにします (したがって、answerb常に定義されます)。

于 2013-10-29T01:39:56.623 に答える
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!")
于 2013-10-29T01:39:45.323 に答える
-1
  1. answerbいいえ、正しく定義していません。else実行されていない可能性のあるステートメント内にあります。if答えが間違っている場合にのみ実行されるため、最後のステートメントをインデントすることが必要だと思います。

  2. question ==複数の使用の代わりにquestion in ['bla1', 'bla2', 'bla3']。ここでは、大文字化は気にしないので、これも必要ありません。question.lower() == 'bla bla bla'可能なすべての大文字と一致するようにすることができます。(他も同様if)

  3. あなたのプログラムの構造はひどいものです。ループを使用して書き直し、質問と回答を配列に保存する必要があります。

于 2013-10-29T01:41:43.567 に答える