2017-03-01 37 views
-4
print("*Space Knowledge Quiz*") 
print() 
print("Question 1:") 
print("Who was the first man to set foot on the Moon?") 
print() 
print("A): Buzz Aldrin") 
print("B): Pete Conrad") 
print("C): Neil Armstrong") 
print("D): Tony Abbot") 
print() 
Answer = input("Input your answer here: ") 
if Answer in ('C', 'Neil Armstrong','c'): 
    print('!! Great Job !!') 
else: 
    print("Oops Try Again? [2 Chances Remaining]") 

#我想用戶有另一個機會2除非正確回答使測驗如何讓用戶擁有多個改掉-python

+0

作爲一般PEP8風格的提示: – Nietvoordekat

回答

0

使用周圍的一切的一個循環與反變量,減少每一次錯誤的嘗試。使用break以正確的答案退出循環。

摘要:

tries = 3 
while tries>0: 
    print('...') 
    Answer = input('...') 
    if Answer in ('correct', 'answers'): 
     print('Yay!') 
     break 
    else: 
     print('Noez!') 
     tries-=1 
-2

你必須創建一個變量 「改掉」 和 「MaxTrys」。

然後你把一個for循環周圍的if語句,像這樣:

for Trys in range(1,MaxTrys): 
    if Answer in ('C', 'Neil Armstrong','c'): 
     print('!! Great Job !!') 
    else: 
     print("Oops Try Again? [2 Chances Remaining]") 

我不是一個Python程序員,我不知道語法是完全地用右手,但我猜您可以在那裏看到邏輯;)

「剩餘2次機會」中的「2」可以替換爲"MaxTrys - Trys"。這會在每次嘗試後更新。

+0

當選擇正確的答案時不會中斷,併發出一個關於其餘試驗的打印錯誤信息 – kaufmanu

0

做一個for chances in range(n)之前stsrting得到用戶輸入。如果用戶是正確的,打印出色的工作並結束程序。在for塊之後,打印用戶沒有更多機會。

很高興幫助解決問題。 :)

(注:n是你想要的機會的數量

0
print("*Space Knowledge Quiz*") 
print() 
print("Question 1:") 
print("Who was the first man to set foot on the Moon?") 
print() 
print("A): Buzz Aldrin") 
print("B): Pete Conrad") 
print("C): Neil Armstrong") 
print("D): Tony Abbot") 
print() 
trials = 3 
for i in range(trials): 
    Answer = input("Input your answer here: ") 
    if Answer in ('C', 'Neil Armstrong','c'): 
     print('!! Great Job !!') 
     break 
    else: 
     remaining = trials-i-1 
     s = '' if remaining == 1 else 's' 
     print("Oops Try Again? [{0} Chance{1} Remaining]".format(remaining, s)) 
+0

'remaining = trials-i-1'看起來相當醜陋,最好試用 - = 1'然後打印試驗。 –

+0

@vishes_shell我同意,但我想保留'trial'的價值,例如如果在此之後出現其他問題。否則,你將不得不記得重置'trials = 3' – kaufmanu

0
print("*Space Knowledge Quiz*\n") 
print("Question 1:") 
print("Who was the first man to set foot on the Moon?\n") 
answerCorrect = False 
maxTrys = 2 
while not answerCorrect and maxTrys > 0: 
    print("(A): Buzz Aldrin") 
    print("(B): Pete Conrad") 
    print("(C): Neil Armstrong") 
    print("(D): Tony Abbot\n") 
    Answer = input("Input your answer here: ") 
    if Answer in ('C', 'Neil Armstrong','c'): 
     print('!! Great Job !!') 
     answerCorrect = True 
    else: 
     if maxTrys == 0: 
      print("Sorry, you lost...") 
     else: 
      print("Oops Try Again? [{} Chances Remaining]".format(maxTrys - 1)) 
     maxTrys -= 1 

**注意這個解決方案是爲Python 3.x的

+0

直到用戶正確回答,這將無限。 –

+0

由於您編輯過,它會進行4次,所以用戶有3次額外的機會,而不是OP所需的2次。 –

相關問題