四則運算(Python)

這個做業屬於哪一個課程 https://edu.cnblogs.com/campus/ahgc/AHPU-SE-19
這個做業要求在哪裏 http://www.javashuo.com/article/p-tgcedkmn-nu.html
這個做業的目標 使用工具來實現四則運算而且提升對需求分析的理解和掌握
學號 3190704131

import randomhtml

from fractions import Fraction
operation = ['+', '-', '*', '/'] #四則運算的符號
global fdom

def integer_score():
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
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 = ''
for 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()code

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)+randorm

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('請從新輸入你的選擇')htm

相關文章
相關標籤/搜索