大佬们帮忙看看怎么才能做到全部答完后才出结果 我这个是做一个出一个
#随机生成十道十以内的加减乘除并给出答案import random
for i in range(11):
a = random.randint(1,10)
b = random.randint(1,10)
c = ['/','*','-','+']
f = random.randint(0,3)
print("%d%s%d="%(a,c,b))
g = int(input())
if f == 0:
ifg == a//b:
print("%d%s%d=%d √"%(a,'//',b,a//b))
else:
print("%d%s%d=%d × 正确答案为 %d"%(a,'/',b,g,a/b))
if f ==1:
if g == a*b:
print("%d%s%d=%d √"%(a,'*',b,a*b))
else:
print("%d%s%d=%d × 正确答案为 %d"%(a,'*',b,g,a*b))
if f ==2:
if g == a-b:
print("%d%s%d=%d √"%(a,'-',b,a-b))
else:
print("%d%s%d=%d × 正确答案为 %d"%(a,'-',b,g,a-b))
if f == 3:
if g == a+b:
print("%d%s%d=%d √"%(a,'+',b,a+b))
else:
print("%d%s%d=%d × 正确答案为 %d"%(a,'+',b,g,a+b))
可以创个列表存储结果和答案再来匹配,例如:
import random
t_list = []
d_list = []
for i in range(11):
a = random.randint(1,10)
b = random.randint(1,10)
c = ['/','*','-','+']
f = random.randint(0,3)
print("%d%s%d="%(a,c,b))
t_list.append(eval("%d%s%d"%(a,c,b)))
g = int(input("请输入答案:"))
d_list.append(g)
print("下一题:\n")
hd = len(t_list)
for k in range(0,hd):
if d_list == t_list:
print("回答正确。")
else:
print("错误,正确答案为:%d"%t_list) import random
d = []
for i in range(11):
a = random.randint(1,10)
b = random.randint(1,10)
c = ['/','*','-','+']
f = random.randint(0,3)
print("%d%s%d="%(a,c,b))
g = int(input())
if f == 0:
ifg == a // b:
d . append("%d%s%d=%d √"%(a,'//',b,a//b))
else:
d . append("%d%s%d=%d × 正确答案为 %d"%(a,'/',b,g,a/b))
if f ==1:
if g == a*b:
d . append("%d%s%d=%d √"%(a,'*',b,a*b))
else:
d . append("%d%s%d=%d × 正确答案为 %d"%(a,'*',b,g,a*b))
if f ==2:
if g == a-b:
d . append("%d%s%d=%d √"%(a,'-',b,a-b))
else:
d . append("%d%s%d=%d × 正确答案为 %d"%(a,'-',b,g,a-b))
if f == 3:
if g == a+b:
d . append("%d%s%d=%d √"%(a,'+',b,a+b))
else:
d . append("%d%s%d=%d × 正确答案为 %d"%(a,'+',b,g,a+b))
for x in range(len(d)):
print('第%2d题 : %s' % (x + 1, d))
本帖最后由 均昊山 于 2020-12-23 20:54 编辑
给你个思路自己改,建个字典:
for循环
a = random.randint(1,10)
b = random.randint(1,10)
c = ['/','*','-','+']
f = random.randint(0,3)
print("%d%s%d="%(a,c,b))这里的输出后在赋值给字典 key
key为运算表达式,value1为用户输入的内容
foe 循环
字典key 用 eval() 函数用来执行一个字符串表达式,并返回表达式的值。与字典value1判断 搭建一个列表储存每个if的结果?用那个往列表添加元素的方法,在每一个if和else下方往列表添加t or f的元素最后提取出来统一?
页:
[1]