import random
score = int(0)
q1 = ("Work out the answer. 45 + 46 = ")
q2 = ("Kai buys a pair of shoes for £31 and a shirt for £67. Altogether, he spends £")
q3 = ("Work out the answer. 68 - 29 = ")
q4 = ("A bike normally costs £260. Its price is reduced in a sale by £90. The sale price of the bike is ")
q5 = ("Work out the answer. 32 x 30 = ")
q6 = ("A box holds 22 apples. The total number of apples in 20 boxes are ")
q7 = ("Work out the answer. 70 x 7 = ")
q8 = ("For a school show, 22 chairs are arranged equally in 30 rows. The total number of chairs is ")
q9 = ("A function machine changes 49 to 98 and changes 26 to '?'. The number in the question mark is ")
q10 = ("What number fills the gap? 35 x ? = 105. The number is ")
question = [
(q1, "91"),
(q2, "98"),
(q3, "39"),
(q4, "170"),
(q5, "960"),
(q6, "440"),
(q7, "490"),
(q8, "660"),
(q9, "52"),
(q10, "3")
]
comments = ["Correct, you get one point, onto the next question", "At least I'm not the only smart one around here",
"Well done", "You must be really smart", "Some people have actually failed at that question",
"Congratulations", "Good work I guess...", "You actually got that right?!"]
random.shuffle(question)
for question, correctanswer in question:
answer = input (question + "")
if answer == correctanswer:
correctA = 1
random.shuffle(comments)
if correctA == 1:
print(comments[random.randrange(len(comments))])
score = score + int(1)
print("Your current score is " + str(score))
else:
print("Wrong, the Correct answer is " + correctanswer)
score = score - int(1)
print("Your current score is " + str(score))
这是更新版本,没有语法错误,所以现在它确实解决了大部分错误,但仍然存在两个错误。 1)在用户输入答案后,什么都没有出现,甚至没有打印得分。 2)评论仅在每个问题得到解答后出现。 我如何解决这两个问题?
答案 0 :(得分:1)
在序列中选择随机元素的一种方法是使用choice function in the random library。
import random
a = [1,2,3]
random.choice(a)
# this chooses a random element in the list a
答案 1 :(得分:0)
正如所建议的那样,只需将所有注释放在一个列表中作为字符串。要打印随机注释,将选择0和列表长度之间的随机数作为打印元素的索引。运行此程序几次以查看它。
import random
comments = ["Correct, you get one point, onto the next question", "At least I'm not the only smart one around here",
"Well done", "You must be really smart", "Some people have actually failed at that question",
"Congratulations", "Good work I guess...", "You actually got that right?!"]
print comments[random.randrange(len(comments))]
答案 2 :(得分:0)
以下是错误:如果答案是正确的,那么您正在创建一个列表..但在创建它时,您使用(并执行)“print”函数,然后打印出所有“正确”的语句条目。
即。从所有列表条目中删除打印功能,然后使用random.choice功能打印其中一个结果集(正如其他人建议的那样)。