有谁知道如何使下面的代码工作? 我希望程序从我的问题列表中随机选择一个问题,直到我遇到5个问题。代码随机选择1个问题,但不重复。
import random
name=input("What is your name?")
score = 0
qno = 0
questionlist = ["1+3", "4-1", "3*3", "5+5", "10*3", "10-4"]
while qno < 10:
qno = qno+1
question = random.choice(questionlist)
print (question)
answer = int (input("what is the answer"))
if answer ==eval(question):
print("correct")
score = score+1
else:
print("incorrect")
答案 0 :(得分:1)
您的代码仅循环遍历while循环的第一个语句。 Python中的缩进用于定义属于你的while,if,for等语句的代码。
以下功能齐全的代码;
import random
name=input("What is your name?")
score = 0
qno = 0
questionlist = ["1+3", "4-1", "3*3", "5+5", "10*3", "10-4"]
while qno < 5:
qno = qno+1
question = random.choice(questionlist)
print (question)
answer = int (input("what is the answer"))
if answer==eval(question):
print("correct")
score = score+1
else:
print("incorrect")
答案 1 :(得分:0)
你的while
循环中唯一的东西就是增加qno
的命令,这将使循环停止,但不是很多。