我正在尝试创建西班牙琐事游戏,但是我很难获得随机问题以接受答案作为该问题的答案。就目前而言,代码最终只是简单地循环回到问题,直到完成所有三个猜测为止。任何帮助将不胜感激。
谢谢!
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!")
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
else:
print("Wah, wah, wahhh! Better luck next time!")
break
我在这里做错了什么?
答案 0 :(得分:0)
您正在以非预期的方式使用python。您正在使用面向对象的编程语言编写过程程序。
我的建议:
为所有问题和答案创建dict
,使用循环保持一段时间不停地提问,并在所有问题用尽时停止,或者循环并随机化所有问题。这样,您只需要一个条件块(即可检查它们是否正确)。
对于计算机,只需从0到计算机不需要回答的每个问题的数量中随机选择一个数字即可,只需随机对或错即可。
答案 1 :(得分:0)
您可以在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后,逐步采用优化的编码方法。
学习愉快!
答案 2 :(得分:0)
程序逻辑中存在一些严重错误。由于代码是从上到下执行的,因此questions
变量将仅生成一次,因此将不断询问相同的问题。同样,答案也从未得到正确检查。您使用if questions == answer
,因为问题和答案不同,所以它永远不会正确。此外,该行将在用户能够输入自己的答案之前执行。
这里是使您步入正轨的示例。我将告诉您如何避免重复的问题以及您打算使用的其他功能。
from random import randint
def generate_question():
question_num = randint(1, 2)
if question_num == 1:
question = "Hola, como estas, Carlos?"
answer = "Hello, how are you, Carlos?"
else:
question = "Me llamo, Mateo!"
answer = "My name is Matthew!"
return question, answer
def main():
guess_count = 0
limit_of_guesses = 3
while guess_count < limit_of_guesses:
# Generate a new question
question, answer = generate_question()
# Ask question and get user's answer
print(question)
users_answer = input("What did the phrase say?")
# Check the answer
if answer == users_answer:
print("You are correct!\n")
else:
print("Wah, wah, wahhh! Better luck next time!\n")
guess_count += 1
print("Gameover!")
main()
答案 3 :(得分:0)
代码从不检查答案,因此不能确定正确性。
当您有大量这样的数据列表时,通常考虑将其放入列表(或从文件加载)是一个好主意。
下面,我将问题列为问答对。可以这样引用:
question, answer = question_text[question_number]
或分别如下:
question = question_text[question_number][0]
answer = question_text[question_number][1]
这给出了更简单的代码。检查答案时,如果计算机注意不要检查多余的空格或单词大写字母,对于用户来说总是比较容易的。在比较中,可以使用python字符串strip()
和lower()
处理此问题。
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)
question_number = randint(1,10)
question_text = [
[ "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.", "You are correct!" ],
[ "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?" ]
]
while starting_line != finish_line:
player_1_progress = starting_line + player1
Computer_progress = computer + Comp_startingline
print(question_text[question_number][0])
if guess_count < limit_of_guesses:
answer = input("What did the phrase say? ")
if (answer.strip().lower() == question_text[question_number][1].strip().lower()):
print("Correct")
question_number = randint(1,10)
else:
guess_count += 1
else:
print("Wah, wah, wahhh! Better luck next time!")
break
答案 4 :(得分:0)
您的代码从根本上是错误的,您应该尝试执行以下操作:
# 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(")
我并不是说这段代码可以完成您想要的一切,但是它将帮助您实现其余的功能。