1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python简单的加法问题_Python实现20以内加减法练习

python简单的加法问题_Python实现20以内加减法练习

时间:2022-12-17 15:49:16

相关推荐

python简单的加法问题_Python实现20以内加减法练习

import random

import operator

def test(n):

correct = 0

wrong = 0

L = []

while (correct + wrong) < n:

for i in range(2):

num = random.randint(1,20)

L.append(num)

L.sort(reverse = True)

op = random.choice("+-+")

#

加法大于

20

,将被重置,所以随机选择运算符的时候,增加加号被选中的机会

if op == "+":

answer = operator.add(L[0],L[1])

elif op == "-":

answer = operator.sub(L[0],L[1])

if answer > 20:

L = []

continue

else:

result = input("\n{} {} {} =

".format(L[0],op,L[1]))

result = int(result)

L = []

if result == answer:

print("

回答正确

")

correct += 1

else:

print("

回答错误

")

wrong += 1

L = []

print("\n

共计

{}

道题,回答正确

{}

题,回答错误

{}

".format(n,correct,wrong))

test(10)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。