基本上我正在嘗試做一個猜謎游戲。有 3 個問題,每個問題你有 3 個猜測。問題是我不擅長編碼,這只是我的第一次。
print("Guessing Game")
player_name = input("Hi! What's your name? ")
number_of_guesses1 = 0
number_of_guesses2 = 0
number_of_guesses3 = 0
guess1 = input("What is the most popular car company in America? ")
while number_of_guesses1 < 3:
number_of_guesses1 = 1
if guess1 == ("Ford"):
break
if guess1 == ("Ford"):
print('You guessed the answer in ' str(number_of_guesses1) ' tries!')
else:
guess2 = input("Try again:")
if guess2 == ("Ford"):
print('You guessed the answer in ' str(number_of_guesses1) ' tries!')
else:
guess3 = input("Try again:")
if guess3 == ("Ford"):
print('You guessed the answer in ' str(number_of_guesses1) ' tries!')
else:
print('You did not guess the answer, The answer was Ford')
guess1b = input("What color do you get when you mix red and brown?")
while number_of_guesses2 < 3:
number_of_guesses2 = 1
if guess1 == ("Maroon"):
break
if guess1b == ("Maroon"):
print('You guessed the answer in ' str(number_of_guesses1) ' tries!')
else:
guess2b = input("Try again:")
if guess2b == ("Maroon"):
print('You guessed the answer in ' str(number_of_guesses1) ' tries!')
else:
guess3b = input("Try again:")
if guess3b == ("Maroon"):
print('You guessed the answer in ' str(number_of_guesses1) ' tries!')
else:
print('You did not guess the answer, The answer was Maroon')
這種代碼是有效的,但前提是你對每個問題都連續兩次得到錯誤的答案,哈哈。我還沒有想到實作計分器的方法(最后我想讓它說你從 3 中得到了多少分。)代碼顯然也沒有完成。基本上,我的問題是:當我回答錯誤一次然后在第二次嘗試時正確回答它說它需要 3 次嘗試時,怎么會這樣?如果你在第一次或第二次嘗試中得到正確的答案,我怎樣才能讓它忽略你剩下的剩余嘗試?這是錯誤代碼,例如,如果我在第二次嘗試時正確:
Traceback (most recent call last):
File "main.py", line 20, in <module>
if guess3 == ("Ford"):
NameError: name 'guess3' is not defined
uj5u.com熱心網友回復:
通過利用 Python 中的資料結構和幾個函式,您可以大大簡化代碼,如下所示:
from collections import namedtuple
# Create a tuyple containing the Question, number of allowed tries and the answer
Question = namedtuple("Question", ["quest", 'maxTries', 'ans'])
def askQuestion(quest: str, mxTry: int, ans: str) -> int:
""" Ask the question, process answer, keep track of tries, return score"""
score = mxTry
while score > 0:
resp = input(quest)
if resp.lower() == ans:
print('Yes, you got it')
break
else:
score -= 1
print(f'Sorry {resp} is incorrect, try again')
if score == 0:
print("Too Bad, you didn't get the correct answer.")
print(f"The correct answer is {ans}")
return score
def playGame():
# Create a list of questions defiend using the Question structure defined above
question_list =[Question('What is the most popular car company in America? ' , 3, 'ford'),
Question("What color do you get when you mix red and brown?", 3, 'maroon')]
plyr_score = 0
for q in question_list:
plyr_score = askQuestion(q.quest, q.maxTries, q.ans)
print(f'Your final score is {plyr_score}')
上述方法允許您擴展問題庫并按問題提供不同數量的最大嘗試次數。
只需執行playGame()即可運行游戲
uj5u.com熱心網友回復:
不要使用不同的變數來跟蹤每個猜測,而是只使用一個變數,并使用input(). 然后你可以一遍又一遍地檢查它,而不用寫一大堆 if-else 陳述句。為了跟蹤數字的正確性,您可以使用一個變數,并在每次輸入正確答案時遞增它。
print("Guessing Game")
player_name = input("Hi! What's your name? ")
score = 0
answer1 = "Ford"
answer2 = "Maroon"
guess = input("What is the most popular car company in America? ")
number_of_guesses = 1
while guess != answer1 and number_of_guesses < 3:
guess = input("Try again: ")
number_of_guesses = 1
if guess == answer1:
print('You guessed the answer in ' str(number_of_guesses) ' tries!')
score = 1
else:
print('You did not guess the answer, The answer was Ford')
guess = input("What color do you get when you mix red and brown? ")
number_of_guesses = 1
while guess != answer2 and number_of_guesses < 3:
guess = input("Try again: ")
number_of_guesses = 1
if guess == answer2:
print('You guessed the answer in ' str(number_of_guesses) ' tries!')
score = 1
else:
print('You did not guess the answer, The answer was Maroon')
print('You got ' str(score) ' out of 2 questions.')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411454.html
標籤:
上一篇:瓷磚圖不顯示
