为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕无忌1623718 2021-11-09 16:36:18
我正在尝试创建一个西班牙语琐事游戏,但我很难让随机问题接受答案作为该问题的答案。就像现在一样,代码最终只是简单地循环回到问题,直到完成所有三个猜测。任何帮助将不胜感激。谢谢!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:    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!")我在这里做错了什么?
查看完整描述

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(")


我并不是说这段代码可以做你想做的一切,但它会帮助你实现其余的功能。


查看完整回答
反对 回复 2021-11-09
?
慕少森

TA贡献2019条经验 获得超9个赞

您正在以一种意想不到的方式使用 python。您正在使用面向对象的编程语言编写过程程序。

我的建议:

创建一个dict所有问题和答案,使用一个循环来继续提问,或者一个 while 循环并在所有问题都用完时停止或 for 循环并随机化所有问题。这样你就只需要一个条件块(检查它们是否正确)。

对于计算机来说,只需从 0 到问题数之间随机生成一个数字,计算机不需要回答每一个问题,它只需要随机对错。


查看完整回答
反对 回复 2021-11-09
?
肥皂起泡泡

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 中进行优化编码。


快乐学习!


查看完整回答
反对 回复 2021-11-09
  • 3 回答
  • 0 关注
  • 208 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信