日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python实现四则运算_python实现的四则运算练习

發布時間:2024/3/12 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实现四则运算_python实现的四则运算练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現的功能:1,面向做題人:即時生成四則運算,等待做題人輸入結果,然后判斷結果是否正確,并給出反饋。

2,面向老師:按照老師輸入的題目數量,生成相應數目的四則運算題目,并給出答案。

代碼:

import random

def szys():

#即時四則遠算函數

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: # 做減法時,要先比較大小,防止輸出負數

n1, n2 = max(n1, n2), min(n1, n2)

result = n1 - n2

elif f == 2:

result = n1 * n2

elif f == 3: # 做除法時,要比較大小,并循環取整除

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():

#生成題庫的函數

sym = ['+', '-', '×', '÷']

print('請輸入所需要的題目數量')

n = int(input())

result = []

m = 0

while m <= (n - 1):

print(m + 1, end='、')

result.append(szys())

print(' ')

m = m + 1

m = 0

print('題目對應的答案:')

while m <= (n - 1):

print(m + 1, '、', result[m])

m = m + 1

print('請選擇模式')

print('1、進行四則運算')

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()

程序運行展示:

總結

以上是生活随笔為你收集整理的python实现四则运算_python实现的四则运算练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。