python实现四则运算_python实现的四则运算练习
實(shí)現(xiàn)的功能:1,面向做題人:即時生成四則運(yùn)算,等待做題人輸入結(jié)果,然后判斷結(jié)果是否正確,并給出反饋。
2,面向老師:按照老師輸入的題目數(shù)量,生成相應(yīng)數(shù)目的四則運(yùn)算題目,并給出答案。
代碼:
import random
def szys():
#即時四則遠(yuǎn)算函數(shù)
sym = ['+', '-', '×', '÷']
f = random.randint(0, 3)
n1 = random.randint(1, 20)
n2 = random.randint(1, 20)
result = 0
if f == 0:
result = n1 + n2
elif f == 1: # 做減法時,要先比較大小,防止輸出負(fù)數(shù)
n1, n2 = max(n1, n2), min(n1, n2)
result = n1 - n2
elif f == 2:
result = n1 * n2
elif f == 3: # 做除法時,要比較大小,并循環(huán)取整除
n1, n2 = max(n1, n2), min(n1, n2)
while n1 % n2 != 0:
n1 = random.randint(1, 10)
n2 = random.randint(1, 10)
n1, n2 = max(n1, n2), min(n1, n2)
result = int(n1 / n2)
print(n1, sym[f], n2, '= ', end='')
return result
def test():
#生成題庫的函數(shù)
sym = ['+', '-', '×', '÷']
print('請輸入所需要的題目數(shù)量')
n = int(input())
result = []
m = 0
while m <= (n - 1):
print(m + 1, end='、')
result.append(szys())
print(' ')
m = m + 1
m = 0
print('題目對應(yīng)的答案:')
while m <= (n - 1):
print(m + 1, '、', result[m])
m = m + 1
print('請選擇模式')
print('1、進(jìn)行四則運(yùn)算')
print('2、制作題庫')
n = int(input())
if n == 1:
while True:
result = szys()
j = input()
s = int(j)
if s == result:
print('right')
else:
print('error.,the answer is', result)
if n == 2:
test()
程序運(yùn)行展示:
總結(jié)
以上是生活随笔為你收集整理的python实现四则运算_python实现的四则运算练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言四则运算报告,C语言四则运算实验报
- 下一篇: css px em rem % vw v