3 回答
TA贡献1839条经验 获得超15个赞
你的代码从根本上是错误的,你应该尝试这样的事情:
# 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(")
我并不是说这段代码可以做你想做的一切,但它会帮助你实现其余的功能。
TA贡献2019条经验 获得超9个赞
您正在以一种意想不到的方式使用 python。您正在使用面向对象的编程语言编写过程程序。
我的建议:
创建一个dict
所有问题和答案,使用一个循环来继续提问,或者一个 while 循环并在所有问题都用完时停止或 for 循环并随机化所有问题。这样你就只需要一个条件块(检查它们是否正确)。
对于计算机来说,只需从 0 到问题数之间随机生成一个数字,计算机不需要回答每一个问题,它只需要随机对错。
TA贡献1829条经验 获得超6个赞
您可以在 while 循环中合并 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 中进行优化编码。
快乐学习!
添加回答
举报