Python:我怎样才能理解输入?

我正在尝试创建一个西班牙语琐事游戏,但我很难让随机问题接受答案作为该问题的答案。就像现在一样,代码最终只是简单地循环回到问题,直到完成所有三个猜测。任何帮助将不胜感激。


谢谢!


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 = "Hello, how are you, Carlos?"

    if questions == answer:

        print ("You are correct!")


elif questions == 2:

    questions = ("Me llamo, Mateo!")

    answer1 = "My name is Matthew!"

    if questions == answer1:

        print ("You are correct!")


elif questions == 3:

    questions = ("Que rabia!")

    answer2 = "What rage!"

    if questions == answer2:

        print ("You are correct!")


elif questions == 4:

    questions = ("Amigo!")

    answer3 = "Friend!"

    if questions == answer3:

        print ("You are correct!")


elif questions == 5:

    questions = ("Me nombre es.")

    answer4 = "My name is."

    if questions == answer4:

        print ("You are correct!")


elif questions == 6:

    questions = ("Le gusta?")

    answer5 = "Do you like him?"

    if questions == answer5:

        print ("You are correct!")


elif questions == 7:

    questions = ("Soy escritor")

    answer6 = "I am a writer."

    if questions == answer6:

        print ("You are correct!")


elif questions == 8:

    questions = ("Me gusta musica!")

    answer7 = "I like music!"

    if questions == answer7:

        print ("You are correct!")


elif questions == 9:

    questions = ("Que estado?")

    answer8 = "What state?"

    if questions == answer8:

        print ("You are correct!")


else:

    questions = ("De donde eres?")

    answer9 = "Where are you from?"

    if questions == answer9:

        print ("You are correct!")


我在这里做错了什么?


慕无忌1623718
浏览 209回答 3
3回答

紫衣仙女

你的代码从根本上是错误的,你应该尝试这样的事情:# The questions that will come up in racingphrases = {    "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(")我并不是说这段代码可以做你想做的一切,但它会帮助你实现其余的功能。

慕少森

您正在以一种意想不到的方式使用 python。您正在使用面向对象的编程语言编写过程程序。我的建议:创建一个dict所有问题和答案,使用一个循环来继续提问,或者一个 while 循环并在所有问题都用完时停止或 for 循环并随机化所有问题。这样你就只需要一个条件块(检查它们是否正确)。对于计算机来说,只需从 0 到问题数之间随机生成一个数字,计算机不需要回答每一个问题,它只需要随机对错。

肥皂起泡泡

您可以在 while 循环中合并 if 条件,如下所示:from random import randintstarting_line = 0Comp_startingline = 0finish_line = 100guess_count = 0limit_of_guesses = 3player_1 = 0player1 = randint(1,10)computer = randint(1,10)questions = randint(1,10)# The questions that will come up in racingif questions == 1:&nbsp; &nbsp; questions = ("Hola, como estas, Carlos?")&nbsp; &nbsp; answer_default = "Hello, how are you, Carlos?"elif questions == 2:&nbsp; &nbsp; questions = ("Me llamo, Mateo!")&nbsp; &nbsp; answer_default = "My name is Matthew!"elif questions == 3:&nbsp; &nbsp; questions = ("Que rabia!")&nbsp; &nbsp; answer_default = "What rage!"elif questions == 4:&nbsp; &nbsp; questions = ("Amigo!")&nbsp; &nbsp; answer_default = "Friend!"elif questions == 5:&nbsp; &nbsp; questions = ("Me nombre es.")&nbsp; &nbsp; answer_default = "My name is."elif questions == 6:&nbsp; &nbsp; questions = ("Le gusta?")&nbsp; &nbsp; answer_default = "Do you like him?"elif questions == 7:&nbsp; &nbsp; questions = ("Soy escritor")&nbsp; &nbsp; answer_default = "I am a writer."elif questions == 8:&nbsp; &nbsp; questions = ("Me gusta musica!")&nbsp; &nbsp; answer_default = "I like music!"elif questions == 9:&nbsp; &nbsp; questions = ("Que estado?")&nbsp; &nbsp; answer_default = "What state?"else:&nbsp; &nbsp; questions = ("De donde eres?")&nbsp; &nbsp; answer_default = "Where are you from?"while starting_line != finish_line:&nbsp; &nbsp; player_1_progress = starting_line + player1&nbsp; &nbsp; Computer_progress = computer + Comp_startingline&nbsp; &nbsp; print(questions)&nbsp; &nbsp; if guess_count < limit_of_guesses:&nbsp; &nbsp; &nbsp; &nbsp; answer = input("What did the phrase say? ")&nbsp; &nbsp; &nbsp; &nbsp; guess_count += 1&nbsp; &nbsp; &nbsp; &nbsp; if answer == answer_default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("You are correct!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print("Wah, wah, wahhh! Better luck next time!")&nbsp; &nbsp; &nbsp; &nbsp; break但是,我鼓励您在完成后慢慢地在 Python 中进行优化编码。快乐学习!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python