我真的需要帮助,这是我的代码下面,我想给每个正确答案随机评论 [英] I really need help, this is my code below and i want to give a random comment for each correct answer

查看:91
本文介绍了我真的需要帮助,这是我的代码下面,我想给每个正确答案随机评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)注释仅在每个问题都得到回答后出现。
如何解决这两个问题?

This is updated version and there are no syntax errors so while it does now fix most of the errors, two errors remain. 1) After the user has inputted an answer, nothing appears, not even the score prints. 2)The comment only appears after every question has been answered. How do i fix both of these problems?

推荐答案

请使用随机库中的选择功能

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

这篇关于我真的需要帮助,这是我的代码下面,我想给每个正确答案随机评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆