大学生在这里和我正在使用python进行我的第一个真正的编码任务之一。主要问题是当它遇到问题1并且用户回答是或否时我得到错误。我认为因为使用了"如果q1 =="是""如果用户输入yes,则应打印下一行,或者如果用户输入no,则应继续下一个问题。任何帮助非常感谢请记住我的第一个编码课程。
def main():
print("this is the common sense game.")
n = eval(input("to begin please type 1: "))
print("question 1: Do they have a fourth of July in England? ")
q1 = eval(input("enter yes or no: "))
if q1 == "yes":
print(" no there is a fourth of July in America GAME OVER.")
print("question 2: are there three outs in an inning of baseball?")
q2 = eval(input("enter yes or no: "))
if q2 == "yes":
print("no there are six GAME OVER")
print("question 3: is it right to ignore a problem until it goes away? ")
q3 = eval(input("enter yes or no: "))
if q3 == "no":
print("solve the problem the moment it occurs don't wait GAME OVER")
else:
print("Common sense is instinct, but enough of it is Genius" "YOU WIN")
main()
main()
答案 0 :(得分:1)
如果您使用的是Python 2,则使用raw_input
,或者使用input
代替{3}}而不是eval(input(
。
这一行:
print("Common sense is instinct, but enough of it is Genius" "YOU WIN")
也有错误。
答案 1 :(得分:0)
您需要将用户输入的结果转换为字符串。在您的代码中,if语句正在测试if q1 == "yes"
,而不是if q1 == some variable named 'yes'
。因此,您需要在问题中将eval()
更改为str()
:
def main():
print("this is the common sense game.")
n = input("to begin please type 1: ")
print("question 1: Do they have a fourth of July in England? ")
q1 = str(input("enter yes or no: "))
if q1 == "yes":
print(" no there is a fourth of July in America GAME OVER.")
print("question 2: are there three outs in an inning of baseball?")
q2 = str(input("enter yes or no: "))
if q2 == "yes":
print("no there are six GAME OVER")
print("question 3: is it right to ignore a problem until it goes away? ")
q3 = str(input("enter yes or no: "))
if q3 == "no":
print("solve the problem the moment it occurs don't wait GAME OVER")
else:
print("Common sense is instinct, but enough of it is Genius" "YOU WIN")
main()
main()