虽然和楼上大神比很小儿科,但也粘出来以供参考吧:
def game():
import random
random.seed(1234)
count = 0
right = 0
while count < 5:
a = random.randint(1,10)
b = random.randint(1,10)
f= random.randint(0, 2)
d = ['+', '-', '*']
print('%d %s %d = ' % (a, d[f], b))
question = input('请输入您的答案:')
if f == 0:
result1 = a + b
if question == str(result1):
print('回答正确')
right += 1
count += 1
else:
print('回答错误')
count += 1
elif f == 1:
result2 = a - b
if question == str(result2):
print('回答正确')
right += 1
count += 1
else:
print('回答错误')
count += 1
else:
result3 = a * b
if question == str(result3):
print('回答正确')
right += 1
count += 1
else:
print('回答错误')
count += 1
percent = right / count
if percent>= 0.8:
print('闯关成功')
else:
print('闯关不成功')