学校で Python を学び始めて約 1 か月が経ちましたが、クイズを作ることにしました。採点システムを追加したので、質問に間違って答えた場合、スコアが表示されます。ただし、これは機能せず、常にスコアが 0 になります。また、質問に失敗した場合に、各質問に 1 つではなく、else ステートメントを 1 つだけ配置する方法はありますか? ありがとう :)
コードの例を次に示します (Python 3.2.3):
#QUIZ
print("Welcome to the quiz")
print("Please choose a difficulty:")
difficulty = input("A) Hard B)Easy")
if difficulty == "A":
score = 0
print("")
def question(score):
print("You chose the hard difficulty")
print("Where is the Great Victoria lake located?")
answer1 = input("A) Canada B)West Africa C)Australia D)North America")
if answer1 == "C":
print("Correct")
score = score+1
else:
print("you failed the quiz")
print("Score:",score)
quit()
def question2(score):
print("Who is most responsible for cracking the Enigma Code")
answer2 = input("A) Alan Turing B) Jeff Bezos C) George Boole D) Charles Babbage")
if answer2 == "A":
print("Correct")
score = score+1
else:
print("you failed the quiz")
print("Score:",score)
quit()
def diff_easy(difficulty):
if difficulty == "B":
score2 = 0
print("")
def question4(score2):
print("You chose the easy difficulty")
print("What is the capital of Australia?")
answer1 = input("A) Canberra B) Sydney C)Melbourne")
if answer1 == "A":
print("Correct")
score2 = score2+1
else:
print("you failed the quiz")
print("Score:",score2)
quit()
def question5(score2):
print("When was the Great Fire of London?")
answer2 = input("A) 1666 B) 1555 C)1605")
if answer2 == "A":
print("Correct")
score2 = score2+1
else:
print("you failed the quiz")
print("Score:",score2)
quit()
if difficulty == "A":
question(score)
question2(score)
if difficulty == "B":
diff_easy(difficulty)
question4(score2)
question5(score2)