我是Python的初学者,我正在创建一个简短的测验。
我创建了一个简短视频,以准确显示正在发生的事情: https://www.youtube.com/watch?v=aHRAr0T-i-Q&feature=youtu.be
我怎么能拥有它,所以它提出了一个问题,“Conor Mcgregor是谁?”然后打印答案,如何阻止它在答案下面再次重复问题?
我的代码:
while True:
question = input("Who is Conor Mcgregor?")
print("A) A Chef")
print("B) A Fighter")
print("C) A Doctor")
if len(question):
answer = question
if answer == "B" or answer == "b":
print("Well Done! Conor Mcgregor is an MMA Fighter.")
break
elif answer == "A" or answer == "C":
print("Wrong Answer!")
break
else:
print("Error")
运行时,其行为如下:
C:\Users\msi\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/msi/PycharmProjects/Reboot/Quiz.py
Who is Conor Mcgregor?
A) A Chef
B) A Fighter
C) A Doctor
Who is Conor Mcgregor?
我怎样才能让它提出问题,然后跟进答案?我必须先按一个按钮才能继续回答,然后当答案显示时,问题就出现了。
提前谢谢你!
答案 0 :(得分:0)
您需要打印问题然后要求输入:
print("Who is Conor Mcgregor?")
print("A) A Chef")
print("B) A Fighter")
print("C) A Doctor")
answer = input('A/B/C?: ')
答案 1 :(得分:0)
你可以用循环
来做while True:
print("Who is Conor Mcgregor? ")
print("A) A Chef")
print("B) A Fighter")
answer = raw_input()
if answer == "B":
print("Correct!")
break
elif answer == "A":
print("Wrong! The correct answer was B! Conor Mcgregor is a Fighter.")
break
print("Please enter a valid answer!")
或使用功能
def question():
print("Who is Conor Mcgregor? ")
print("A) A Chef")
print("B) A Fighter")
answer = raw_input()
if answer == "B":
print "Correct!"
return True
elif answer == "A":
print "Wrong! The correct answer was B! Conor Mcgregor is a Fighter."
return True
print("Please enter a valid answer!")
question()
question()
如果使用python 2.7
,还需要更改raw_input的输入