def ask_questions():
choice = (random.choice(question))
print(choice)
if choice == 0:
print options[0]
answer0 = raw_input(inputs)
if answer0 == answers[0]:
print("correct")
else:
print("incorrect")
elif choice == 1:
print choice
print options[1]
answer1 = raw_input(inputs)
if answer1 == answers[1]:
print("correct")
else:
print("incorrect")
elif choice == 2:
print choice
print options[2]
answer2 = raw_input(inputs)
if answer2 == answers[2]:
print("correct")
else:
print("incorrect")
elif choice == 3:
print choice
print options[3]
answer3 = raw_input(inputs)
if answer3 == answers[3]:
print("correct")
else:
print("incorrect")
elif choice == 4:
print choice
print options[4]
answer4 = raw_input(inputs)
if answer4 == answers[4]:
print("correct")
else:
print("incorrect")
elif choice == 5:
print choice
print options[5]
answer5 = raw_input(inputs)
if answer5 == answers[5]:
print("correct")
else:
print("incorrect")
def main()
date()
quiz_infos()
welcome()
ask_questions()
main()
我想知道一种从列表中随机选择问题的方法,如果问题是1:打印选项1和我的raw_input(输入)同样适用于问题2 3 4等 idk为什么我的代码实际上并没有这样做,只打印问题,所以如果elif函数不起作用! 我刚接触python(新编码)所以我可能肯定做错了, 通过变量的方式[[[[输入="您认为答案是什么?"]]]] 谢谢你! 代码是用python 2.7 idle
编写的答案 0 :(得分:1)
有更简单的方法可以做到这一点,但是这里是你为python 3语法修改的确切代码和适当的缩进(如果列表"问题","选项"和& #34;答案"以及常量"输入"都是实际定义的而不是空的。)
def ask_questions():
choice = (random.choice(question))
if choice == 0:
print(choice)
print(options[0])
answer0 = input(inputs)
if answer0 == answers[0]:
print("correct")
else:
print("incorrect")
elif choice == 1:
print(choice)
print(options[1])
answer1 = input(inputs)
if answer1 == answers[1]:
print("correct")
else:
print("incorrect")
等...
C:\Users\me\Documents>python test.py
0
Question 1
User Input: Answer 1
correct
C:\Users\me\Documents>python test.py
4
Question 5
User Input: Answer 5
correct
正如我上面所说,如果你的代码中没有,你在提交中粘贴的内容会有缩进错误。如果您的代码在打印问题后挂起,我怀疑您的输入"可能是一个空字符串。
答案 1 :(得分:0)
我个人会提出每个问题:在字典中回答
Questions = {"What is your favorite Color?":"Blue","How many cats do I own?": "2"}
然后你可以使用KEYS方法从列表中返回一个随机选择
import random
Questions = {"What is your favorite Color?":"Blue","How many cats do I own?": "2"}
random.choice(Questions.keys())
您可以在此处查找字典信息:Dictionary 此处还有随机模块Random