我真的需要帮助,这是我下面的代码,我想给每答对一个随机评论(I really need help,

2019-10-23 14:11发布

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)每一个问题已经回答后,评论才会出现。 如何解决这两个问题?

Answer 1:

选择序列中的随机元素的一种方法是使用过的选择功能在随机库 。

import random
a = [1,2,3]
random.choice(a)
# this chooses a random element in the list a


Answer 2:

至于有人建议,干脆把所有的评论列表中的字串。 要打印的随机评论,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))]


Answer 3:

下面是错误:如果答案是正确的,你正在创建一个列表..但在创造它,您可以使用(和执行)的功能“打印”,这反过来又打印出你的“正确”的语句所有条目。

即。 从所有列表中的条目删除打印功能,而只打印结果使用random.choice功能设置中的一个(如其他人所说)。



文章来源: I really need help, this is my code below and i want to give a random comment for each correct answer