四則運算生成器


相對於上一個版本,此版本支持浮點數運算,支持交互式操作,支持錯誤校驗並計算成績,略微遺憾的是不支持分數計算。

 

碼市鏈接:https://git.coding.net/poisonous-ant/sizeyunsuan.git

 

源代碼如下:

import random
def Subject_Creater():
i = 0
a = 0
b = 0
print "How many questions do you want to test ?"
n = input()
while i<n : # 隨機生成的題目數量為用戶輸入的數
x = round(random.uniform(0,10),2)
y = round(random.uniform(0,10),2)
z = random.choice("+-*/")
if z == "+":
result = round(x+y,2)
print x, z, y, "="
elif z == "-":
result = round(x-y,2)
print x, z, y, "="
elif z == "*":
result = round(x*y,2)
print x, "×", y, "="
else :
if y == 0:
i -= 1
else:
result = round(x/y,2)
print x, "÷", y, "="
temp = input()
if result == temp: # 用於記錄正確和錯誤答題數以便給出成績
print "Ture"
a += 1.0
else:
print "False ! The truth answer is:",result
b += 1.0
i += 1
print "Your grade is :",round(a/(a+b)*100,2)

Subject_Creater()

運行截圖:

 

托管到Git:

另外:推薦一個超級簡單的Git教程:

http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

 

個人水平不足,歡迎大家指教。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM