python程序设计基础,题目我放在图片里请大家看一下谢谢大家
import random
str1 = ""
str2 = ""
for i in range(1,31):
num1 = random.randint(10, 99) # 随机产生数1
num2 = random.randint(10, 99) # 随机产生数2
symbol = random.choice("+-")
# 判断符号是加号 还是减号
if symbol == "+":
result = num1 + num2 # 是+号 做加法
else:
if num1 < num2:
num1, num2 = num2, num1
result = num1 - num2 # 否则做减法
num1 = str(num1).ljust(2, " ")
num2 = str(num2).ljust(2, " ")
express1 = num1 + " " + symbol + " " + num2 + " = "
express2 = num1 + " " + symbol + " " + num2 + " = " + str(result).ljust(2, " ")
if i % 5 == 0:
str1 += express1 + "\n"
str2 += express2 + "\n"
else:
str1 += express1 + "\t"
str2 += express2 + "\t"
with open("math.txt", "w", encoding="utf8") as file:
file.write(str1)
with open("key.txt", "w", encoding="utf8") as file:
file.write(str2)
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!