python四则运算代码_四则运算Python代码import random
from fractions import Fraction
operation = ['+', '-', '*', '/'] #四则运算的符号
global f
def integer_score():
#rand = operation[random.randint(0,3)]
number = random.randint(1,4) #随机产⽣的表达式长度
f = ''
for i in range(number):
a = random.randint(1,20) #随机产⽣的表达式中的数
rand = operation[random.randint(0, 3)] #随机选择⼀个四则运算中的符号
if rand == '/':
b = random.randint(a, 20) #随机产⽣的真分数的分母
f += str(a) + rand + str(b) #数与符号相连
rand = operation[random.randint(0, 2)] #随机选择⼀个四则运算中的符号
f += rand
else:
f += str(a) + rand
#print(a,rand,end='')
b = random.randint(1, 20)
f += str(b) #得到完整的表达式
n = eval(f) #得到表达式的结果
n = Fraction('{}'.format(n)).limit_denominator() #⼩数转化为分数
if n > 0:
print('题⽬:')
print(f,'=')
print('请输出答案:')
x = Fraction('{}'.format(eval(input()))).limit_denominator()
if n == x: #输⼊的数与表达式⽐较
print(True)
else:
print(False)
print('正确的答案为:',n)
else:
integer_score()
def integer():
# rand = operation[random.randint(0,3)] number = random.randint(1, 3)
f = ''
random pythonfor i in range(number):
a = random.randint(1, 10)
rand = operation[random.randint(0, 3)] f += str(a) + rand
b = random.randint(1, 10)
f += str(b)
n = eval(f)
if isinstance(n, int) and n > 0:
print('题⽬:')
print(f, '=')
print('请输出答案:')
x = eval(input())
if n == x:
print(True)
else:
print(False)
print('正确的答案为:', n)
else:
integer()
def score():
op = ['+', '-']
number = random.randint(1, 3)
f = ''
for i in range(number):
a = random.randint(1, 10)
b = random.randint(a, 10)
rand = op[random.randint(0, 1)]
f += str(a) + '/'+ str(b)+rand
a = random.randint(1, 10)
b = random.randint(a, 10)
f += str(a) + '/'+ str(b)
n = eval(f)
n = Fraction('{}'.format(n)).limit_denominator()
if n > 0:
print('题⽬:')
print(f,'=')
print('请输出答案:')
x = Fraction('{}'.format(eval(input()))).limit_denominator() if n == x:
print(True)
else:
print(False)
print('正确的答案为:',n)
else:
score()
if __name__ == '__main__':
while True:
print('选择你想做的题⽬:')
print('0(退出)1(分数题⽬),2(整数题⽬),3(综合题⽬)')
m = int(input())
if m == 1:
score()
elif m == 2:
integer()
elif m == 3:
integer_score()
elif m == 0:
exit()
else:
print('请重新输⼊你的选择')
#isinstance(1, int)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论