我有一個測驗,在最后一輪,如果它的值不正確,我希望它結束
我有一個if回圈,但我希望我的最終else值能夠轉到行號代碼,因為它是一個長代碼。為了使用此功能,我如何查看行號(我使用Portable Python Scripter)。
print("PLEASE NOTE: Answers are case-sensitive, so start all answers with a capital letter.")
print("Getting full marks (5/5) in each round will allow you to complete the next round.")
#==============================================================================
singer = input("Who is your favourite singer?")
if singer == "Harry Styles" or "Harry":
print("Great choice!")
else:
print(singer " is good, but Harry Styles is better")
#==============================================================================
ans = input("Do you want to do a Harry Styles quiz?")
if ans == "No":
print("How rude!")
elif ans == "Yes":
#==============================================================================
q1 = input("What boyband was Harry Styles in?")
if q1 == "One Direction":
print("Correct!")
score = 1
elif q1 == "1D":
print("Correct!")
score = 1
elif q1 == "White Eskimo":
print("Correct!")
score = 1
else:
print("Incorrect")
score = 0
#==============================================================================
q2 = input("What is Harry's birthday (write in format dd/mm/yyyy)")
if q2 == "01/02/1990":
print("Correct!")
score = score 1
else:
print("Incorrect")
#==============================================================================
q3 = input("What is Harry's favourite film?")
if q3 == "Love, Actually":
print("Correct!")
score = score 1
elif q3 == "Love Actually":
print("Correct!")
score = score 1
else:
print("Incorrect")
#==============================================================================
q4 = input("What war film did Harry star in?")
if q4 == "Dunkirk":
print("Correct!")
score = score 1
else:
print("Incorrect")
#==============================================================================
q5 = input("What is Harry's mum's first name?")
if q5 == "Anne":
print("Correct!")
score = score 1
else:
print("Incorrect")
#==============================================================================
if score < 5:
ps = str(score)
print("You achieved " ps)
if score == 5:
print("Well done! You got everything correct!")
print("Entering quickfire round...")
#==============================================================================
score = 0
print("Instructions:")
print("Your score has been reset to 0")
print("If you get a question incorrect, you will lose 1 point")
print("If you achieve below 0, you wil end the game.")
#==============================================================================
while score <= 0:
qf1 = input("What is Harry's 2nd album called?")
if qf1 == "Fine Line":
print("Correct")
score = score 1
print(score)
else:
print("Incorrect")
score = score - 1
print(score)
#==============================================================================
qf2 = input("How old was Harry when he went on the X-Factor?")
if qf2 == "16":
print("Correct")
score = score 1
print(score)
elif qf2 == "Sixteen":
print("Correct")
score = score 1
print(score)
else:
print("Incorrect")
score = score - 1
print(score)
#==============================================================================
qf3 = input("Finish the quote: 'This is a family show. _________?'")
if qf3 == "Or is it?":
print("Correct")
score = score 1
print(score)
elif qf3 == "Or is it":
print("Correct")
score = score 1
print(score)
else:
print("Incorrect")
score = score - 1
print(score)
#==============================================================================
qf4 = input("What is Harry's middle name?'")
if qf4 == "Edward":
print("Correct")
score = score 1
print(score)
else:
print("Incorrect")
score = score - 1
print(score)
#==============================================================================
qf5 = input("What is Harry's blood type?'")
if qf5 == "B ":
print("Correct")
score = score 1
print(score)
elif qf5 == "B positive":
print("Correct")
score = score 1
print(score)
else:
print("Incorrect")
score = score - 1
print(score)
#==============================================================================
if score < 0:
print("GAME OVER!")
#==============================================================================
if score > 0:
if score < 5:
ps = str(score)
if score == 5:
print("Wow, that's full marks! Well done!")
#==============================================================================
print("Entering Death Row...")
score = 0
print("Instructions:")
print("Your score has been reset to 0")
print("If you get a question incorrect, you will end the game")
#==============================================================================
d1 = input("What characer does Harry play in the MCU (Marvel Cinematic Universe)?")
if d1 == "Starfox" or "Eros":
print("Correct!")
score = score 1
#==============================================================================
d2 = input("What does Harry say the best thing a girl can wear is?")
if d2 == "A smile" or "Smile":
print("Correct!")
score = score 1
#==============================================================================
d3 = input("What did Harry dress up as for 2021's 'Harryween'?")
if d3 == "Alice" or "Alice in Wonderland" or "A clown" or "Clown":
print("Correct!")
score = score 1
#==============================================================================
d4 = input("Which singer does Harry have a crush on?")
if d4 == "Adele":
print("Correct!")
score = score 1
#==============================================================================
d5 = input("How many nipples does Harry have?")
if d5 == "Four" or "4":
print("Correct!")
score = score 1
#==============================================================================
if score == 5:
print("Oh ma gawd! You got everything correct!")
print("I think you dropped this [??]")
print("Get your certificate here: [https://drive.google.com/file/d/1k-PJEQmqW_rv56vSId0PDo6etuC_up_G/view]")
#==============================================================================
else:
ps = str(score)
print("Incorrect")
print("You achieved " score)
else:
ps = str(score)
print("Incorrect")
print("You achieved " score)
else:
ps = str(score)
print("Incorrect")
print("You achieved " score)
else:
ps = str(score)
print("Incorrect")
print("You achieved " score)
else:
print("Incorrect")
#==============================================================================
else:
termination = (ans.upper())
print("ERROR! CANNOT COMPUTE! UNKNOWN VARIABLE: [" termination "]")
print("TERMINATING QUIZ!") ```
uj5u.com熱心網友回復:
使用回圈和函式可以幫助你使這段代碼更短一些,并消除很多復制 粘貼 if/else 的需要。這是對初始測驗部分的快速重寫,其中包含一個main()函式的概要,以便為您提供想法:
def first_round():
"""Ask the first round questions, return cumulative score."""
score = 0
for question, answers in [
("What boyband was Harry Styles in?",
{"One Direction", "White Eskimo"}),
("What is Harry's birthday (write in format dd/mm/yyy)",
{"01/02/1990"}),
("What is Harry's favourite film?",
{"Love, Actually", "Love Actually"}),
("What war film did Harry star in?",
{"Dunkirk"}),
("What is Harry's mum's first name?",
{"Anne"}),
]:
if input(question) in answers:
print("Correct!")
score = 1
else:
print("Incorrect")
return score
def main():
# do intro stuff here
score = first_round()
if score < 5:
print(f"You achieved {score}")
return # Game over!
print("Well done! You got everything correct!")
print("Entering quickfire round...")
# continue on with score = quickfire_round()...
main()
將代碼放在函式中意味著當需要結束時,您可以return讓函式的其余部分在縮進的外部級別繼續,而不必進入越來越深的嵌套if/elif/else陳述句來繼續程式。使用回圈(如first_round上面的函式)意味著您不需要一遍又一遍地復制和粘貼相同的代碼。
采用上面的模式并將其應用于死亡回合,我們可能會得到類似的結果:
def death_round():
"""Ask the death round questions -- first wrong answer ends it!"""
score = 0
for question, answers in [
("What characer does Harry play in the MCU (Marvel Cinematic Universe)?",
{"Starfox", "Eros"}),
("What does Harry say the best thing a girl can wear is?",
{"A smile", "Smile"}),
("What did Harry dress up as for 2021's 'Harryween'?",
{"Alice", "Alice in Wonderland", "A clown", "Clown"}),
("Which singer does Harry have a crush on?",
{"Adelete"}),
("How many nipples does Harry have?",
{"Four", "4"}),
]:
if input(question) in answers:
print("Correct!")
score = 1
else:
print("Incorrect")
return score # sudden death!
return score
請注意return在“不正確”的情況下,這是此函式與 不同的一種方式first_round,也是它的原因,因此您不需要一個goto或五個不同的嵌套else陳述句來跟蹤可能結束游戲的五個不同時間-- 只要有一個錯誤的答案,函式就會回傳,然后main可以查看最終分數并根據它的值做不同的事情。
在您進行測驗時要考慮的另一件事 - 現在它對準確的措辭和大小寫非常敏感,即使您提供了多個正確的選項。例如:
What does Harry say the best thing a girl can wear is?Her smile
Incorrect
這是修復它的一種方法,可以使任何包含“微笑”一詞的答案都被接受,無論大小寫:
for question, answers in [
("What does Harry say the best thing a girl can wear is?",
{"smile"}),
("What did Harry dress up as for 2021's 'Harryween'?",
{"alice", "clown"}),
]:
answer = input(question).lower()
if any(a in answer for a in answers):
print("Correct!")
else:
print("Incorrect")
What does Harry say the best thing a girl can wear is? Just a smile
Correct!
What did Harry dress up as for 2021's 'Harryween'? A fabulous clown
Correct!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/379737.html
下一篇:如何使用R中的條件過濾?
