3 回答

TA貢獻1839條經(jīng)驗 獲得超15個贊
你的代碼從根本上是錯誤的,你應(yīng)該嘗試這樣的事情:
# The questions that will come up in racing
phrases = {
"Hola, como estas, Carlos?": "Hello, how are you, Carlos?",
"Me llamo, Mateo!": "My name is Matthew!",
"Que rabia!": "What rage!",
"Amigo!": "Friend!",
"Me nombre es.": "My name is.",
"Le gusta?": "Do you like him?",
"Soy escritor": "I am a writer.",
"Me gusta musica!": "I like music!",
"Que estado?": "What state?",
"De donde eres?": "Where are you from?"
}
for phrase, answer in phrases.items():
while not input(f"What does that mean:\n{phrase}\n> ") == answer:
print("Wrong answer try again ! :o(")
我并不是說這段代碼可以做你想做的一切,但它會幫助你實現(xiàn)其余的功能。

TA貢獻2019條經(jīng)驗 獲得超9個贊
您正在以一種意想不到的方式使用 python。您正在使用面向?qū)ο蟮木幊陶Z言編寫過程程序。
我的建議:
創(chuàng)建一個dict
所有問題和答案,使用一個循環(huán)來繼續(xù)提問,或者一個 while 循環(huán)并在所有問題都用完時停止或 for 循環(huán)并隨機化所有問題。這樣你就只需要一個條件塊(檢查它們是否正確)。
對于計算機來說,只需從 0 到問題數(shù)之間隨機生成一個數(shù)字,計算機不需要回答每一個問題,它只需要隨機對錯。

TA貢獻1829條經(jīng)驗 獲得超6個贊
您可以在 while 循環(huán)中合并 if 條件,如下所示:
from random import randint
starting_line = 0
Comp_startingline = 0
finish_line = 100
guess_count = 0
limit_of_guesses = 3
player_1 = 0
player1 = randint(1,10)
computer = randint(1,10)
questions = randint(1,10)
# The questions that will come up in racing
if questions == 1:
questions = ("Hola, como estas, Carlos?")
answer_default = "Hello, how are you, Carlos?"
elif questions == 2:
questions = ("Me llamo, Mateo!")
answer_default = "My name is Matthew!"
elif questions == 3:
questions = ("Que rabia!")
answer_default = "What rage!"
elif questions == 4:
questions = ("Amigo!")
answer_default = "Friend!"
elif questions == 5:
questions = ("Me nombre es.")
answer_default = "My name is."
elif questions == 6:
questions = ("Le gusta?")
answer_default = "Do you like him?"
elif questions == 7:
questions = ("Soy escritor")
answer_default = "I am a writer."
elif questions == 8:
questions = ("Me gusta musica!")
answer_default = "I like music!"
elif questions == 9:
questions = ("Que estado?")
answer_default = "What state?"
else:
questions = ("De donde eres?")
answer_default = "Where are you from?"
while starting_line != finish_line:
player_1_progress = starting_line + player1
Computer_progress = computer + Comp_startingline
print(questions)
if guess_count < limit_of_guesses:
answer = input("What did the phrase say? ")
guess_count += 1
if answer == answer_default:
print ("You are correct!")
break
else:
print("Wah, wah, wahhh! Better luck next time!")
break
但是,我鼓勵您在完成后慢慢地在 Python 中進行優(yōu)化編碼。
快樂學(xué)習(xí)!
添加回答
舉報