[Python] in Python to train math?

[Python] in Python to train math?
Math is very important, today we have to achieve to practice mathematical calculations in conjunction with Python random library.
It's renderings
So how should we do it? With the view of the code

# By Forty / Three % Two
import random
right = 0     # 正确回答数
kacha = 0     # 错误回答数
zongshu = 0   # 总回答数
print("说“0”可以退出并且得到统计信息哦!")
while True:
    try:
        fuhao = ["+","-","*","/"]      # 运算符号
        a = round(random.uniform(1,1000))                          # 生成随机数
        b = round(random.uniform(1,1000))                          # 生成随机数
        c = random.choice(fuhao)                                   # 随机选择符号
        if c == "+":    # 如果运算符号是+
            temp = input("a = {},b = {} a+b=?".format(a,b))   # 获取回答
            temp = int(temp)
            zongshu += 1
            if temp == a + b:
                print("答对了!")
                right += 1
            elif temp == 0:
                print("您一共计算了{}题,其中正确{}题,错误{}题".format(zongshu,right,kacha))
                break
            else:
                print("答错了!")
                kacha += 1

        if c == "-":
            temp = input("a = {},b = {} a-b=?".format(a,b))
            temp = int(temp)
            zongshu += 1
            if temp == a - b:
                print("答对了!")
                right += 1
            elif temp == 0:
                print("您一共计算了{}题,其中正确{}题,错误{}题".format(zongshu,right,kacha))
                break
            else:
                print("答错了!")
                kacha += 1

        if c == "*":
            temp = input("a = {},b = {} a*b=?".format(a,b))
            temp = int(temp)
            zongshu += 1
            if temp == a * b:
                print("答对了!")
                right += 1
            elif temp == 0:
                print("您一共计算了{}题,其中正确{}题,错误{}题".format(zongshu,right,kacha))
                break
            else:
                print("答错了!")
                kacha += 1

        if c == "/":
            temp = input("a = {},b = {} a//b=?".format(a,b))   # 获取商
            temp = int(temp)
            temp2 = input("a%b=?")                            # 获取余数
            temp2 = int(temp)
            if temp == a // b and temp2 == a % b:
                print("答对了!")
                right += 1
            elif temp == 0:
                print("您一共计算了{}题,其中正确{}题,错误{}题".format(zongshu,right,kacha))
                break
            else:
                print("答错了!")
                kacha += 1
    except:
        print("请输入合法的答案")

So there are actually lots of knowledge, such as randomly generated numbers, try ... except, if statements, input and so on . So we have a little bit of say

First, we look at how to generate the random number
Here Insert Picture Description
herein refers to a addend (minuend / factor / dividend), b is the same, it is the red box generates a random number code. Use round function, which is written in the random.uniform function. round function, there are two parameters, the first is a uniform function, the second generated random number has several decimal places. And we do not need to decimals , you can not write. In random.uniform two parameters in function is made? To? The random number . We need three digits, then write 1,1000.
(Some variables mean I do not write, we can see the code comments)
Here Insert Picture Description
Next, we want to use the choise of random selection method random operation symbol ** (method is to put a list of names in parentheses) **

So now, our preparations have been completed it, take a look at the code judging portion of it!
PS: Do not forget to add a while loop and try Oh!
Oh, and try ... except usage:
Following the try is content to run behind except if the error is, and how to deal with
Here Insert Picture Description
the following very simple. We look at the judgment operation symbol
(Because there are four operational sign, so here wrote four if, if there is an easier way to welcome the guidance)
Here Insert Picture Description
to obtain input with input and converted to an int
and then to judge, to say here == 0 is the TEMP
0 is the exit, so we have to add break exits the loop, and print billing information. As for the format of usage can inform themselves about.
The other is very much the same operational sign change it on the line.
Especially want to say is that the division
Here Insert Picture Description
can be seen through my notes, is a business, is a remainder. And then if we have to add in a condition.
Here Insert Picture Description
And then were judged, but also very much the same basic
Here Insert Picture Description
Here we must speak of except
we want to, if the user does not enter or is not a number, the program is being given! So we have to use it to capture the error , and print message

Well, today's teaching on here, I was concerned about, and share with you interesting little program! ┏ (^ 0 ^) ┛

Published an original article · won praise 3 · Views 117

Guess you like

Origin blog.csdn.net/weixin_46360180/article/details/104395511