楼主:
FrockYu (陈俊杰铁粉)
2020-05-15 01:13:39安安,各位程式高手,最近在自学python ,然后今天做到一题百思不得其解,想请问各位
题目大概的叙述是,要设计随机的加减法问题,让使用者输入想答题的题数,然后当使用者答对,印出"good job",若是使用者答错,一题有3次机会尝试,若第三次仍答错,则会印出"答案应该是___"
我大致上把程式架构都写完了,答对输出OK,答错会输出正确答案也OK,可是就是一直搞不定一题可以尝试3次这部分,想请问各位该怎么解? (然后因为目前只自修到布林运算,所以希望可以用布林运算 while for if等方法解释,谢谢!)
以下是程式码:
n = int(input('Number of problems: '))
pass_ = 0 #答对题数
fail_ = 0 #答错题数
count = 0 #单题答错次数
import random
for x in range(n):
op1 = random.randint(10,100)
op2 = random.randint(10,100)
operator = random.choice(['+','-'])
if operator == '+':
solution = op1 + op2
else:
if op1 < op2:
op1,op2 = op2,op1
solution = op1 - op2
print(op1,operator,op2,'= ? ',end='')
answer = int(input())
if answer == solution:
print('Good job!')
pass_ += 1
continue
else:
while count < 3:
count += 1
fail_ += 1
if count == 3:
fail_ += 1
print('Sorry, should be ',solution)
print('Final score: ', 100 * pass_ / (pass_ + fail_))