20190919-6 four operations to generate questions

See also operational requirements: https://edu.cnblogs.com/campus/nenu/2019fall/homework/7631 .

Code Address: https://e.coding.net/wangkefei/f4.git .

 

1. The four operational functions: to support the four operations subject title number 4, all of the questions asked have the ability to correctly answer.

Key: introducing random module, using random () function to generate random numbers and operators. eval () function to achieve the correct answer is calculated expression, compared with the result of user input, that is equal to the correct answer, otherwise an error.

Define the variable k is used to calculate the number of correct answers.

 

Code:

Import Random 


DEF Generate (): 

    OPE = [ ' + ' , ' - ' , ' * ' , ' / ' ] 
    num1 = the random.randint (. 1, 10 ) randomly generated number between # 1-10 
    num2 = Random. the randint (. 1, 10 ) 
    num3 = the random.randint (. 1, 10 ) 
    Num4 = the random.randint (. 1, 10 ) 
    ope1 = The random.choice (OPE) # operator randomly generated 
    OPE2 = The random.choice (OPE) 
    ope3 = random.choice(ope)
    exp = str(num1) + str(ope1) + str(num2) + str(ope2) + str(num3) + str(ope3) + str(num4)
    return exp


ret = []
for i in range(20):
    ret.append(generate())
k = 0
for el in ret:
    print(el)
    print('?', end="")
    answer = float(input())
    if answer == eval(el):     # eval()计算表达式的值
        k += 1
        Print ( ' answer you, you're a genius! ' )
     the else :
         Print ( ' Think again, the answer seems to be% d Oh! ' % (eval (EL)))
 Print ( ' you a total of% d answer questions, a total of 20 questions. ' % K)

 

Run shot:

 

2. Support function parentheses.

Key: Define a brace list to randomly generate a list of index indicates generate a left parenthesis, right parenthesis or brackets are not generated. Generating a position in parentheses indicates a multilayer judgment.

Code:

    OPE = [ ' + ' , ' - ' , ' * ' , ' / ' ] 
    BRA = [ ' ( ' , '' , ' ) ' ]       # 0: generating a left parenthesis 1: does not generate bracket 2: generate a right bracket 
    num1 the random.randint = (. 1, 10 ) 
    num2 = the random.randint (. 1, 10 ) 
    num3 = the random.randint (. 1, 10 ) 
    Num4 = the random.randint (. 1, 10 ) 
    ope1 = The random.choice (OPE) 
    OPE2= The random.choice (OPE) 
    ope3 = The random.choice (OPE) 

    LEFT1 = the random.randint (0,. 1 ) 
    LEFT2 = the random.randint (0,. 1 ) 
    left3 = the random.randint (0,. 1 ) 
    RIGHT1 = the random.randint (. 1, 2 ) 
    RIGHT2 = the random.randint (. 1, 2 ) 
    Right3 = the random.randint (. 1, 2 )
     IF LEFT1 == 0: # of bracket pairs occurs 
        LEFT2 =. 1 
        left3 =. 1
         IF RIGHT1 == 2 : 
            RIGHT2 =. 1
            right3 = 1
        else:
            right2 = 2
            right3 = 1
    else:
        if left2 == 0:
            left3 = 1
            right1 = 1
            if right2 == 2:
                right3 = 1
            else:
                right3 = 2
        else:
            left3 = 0
            right1 = 1
            right2 = 1
            right3 = 2
    exp = BRA [LEFT1] + STR (num1) + STR (ope1) + BRA [LEFT2] + STR (num2) + \ # add location parentheses may appear in the function of an arithmetic formula 
          BRA [RIGHT1] + STR (OPE2 ) + BRA [left3] + STR (num3) + BRA [RIGHT2] + \ 
          STR (ope3) + STR (Num4) + BRA [Right3]
     return exp

Run shot:

 

3. function defines the number of items, "fine" printout, to avoid duplication.

Key: argparse () module, used for parsing command line parameters and options.

 Key Code:

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("-c", "--cin")
    args = parser.parse_args()
    if args.cin is None:
        f4().unlimited()
    else:
        f4().limited(args.cin)

 Run shot:

 

4. Support the topic scores and operations.

Focus: the use of fractions module, to perform mathematical operations included scores.

 Key Code:

        num1 = random.uniform(0, 1)
        num1 = random.Fraction(num1).limit_denominator(10)
        num2 = random.uniform(0, 1)
        num2 = random.Fraction(num2).limit_denominator(10)

 

    def answer(self, eq):
        from fractions import Fraction as f
        answer = f(eval(eq)).limit_denominator(1000)
        answer = str(answer)
        return answer

 

Run shot:

 

 

Summary and Experience:

1. Junction experience of programming

  The first time two people programming team, more direct, more efficient than the team during more than undergraduate. Two people sitting together using the same computer to communicate with faster this way, especially his poor understanding of the problem, two people can pass on. First, the existing whole idea is very important, two people decided to write after a discussion with python language. Ever writing code yourself, there is often a bug to change, however, can not be achieved or function of the situation but could not find the cause. Co-authored two people will be more of an idea, when a person "blind alley" can provide more than one way. The job for me some difficulty, at first the idea is not clear, python is a beginner's language, but fortunately relatively easy to read, is not very complicated, but also spent a lot of time longer than imagined.

 

2. At least five spent a long time in coding, debate, review and other activities, to give you a larger harvest event

(1) understand the python function takes a long time but the greatest gains. python has many built-in functions to achieve a lot of features, such as the job eval (), random (), etc., and their syntax lookup functions through the Internet, use the function in the program, more convenient.

(2) after the beginning of the title finished browsing think the point is that RPN, so direct search python RPN-related knowledge, understand the principles infix turn suffix, brackets and operator push and popped, and also spent longer time. But that comes later found python eval () function can let the computer automatically arithmetic expressions with parentheses, no RPN can also calculate the correct result. Although RPN not be applied to code, but to understand the principles, but also a great harvest.

(3) features two brackets added difficult to achieve, it takes a long time. Do not start thinking, suggesting the idea of ​​brothers, it was in the past and reference code brothers, modified and incorporated into a function in it can be achieved.

(4) in the programming process, the thinking brain becomes for me the most difficult thing the actual code. Since the programming ability is too weak, even with ideas, it is difficult to practice it with both hands. Fortunately, the process of pair programming, the companion can give help, though it takes a long time, but there is a great harvest.

(5) function adds four argparse () module, parse command line arguments and options used for a longer time spent on the understanding module and to implement them, but it is also the biggest event of the harvest.

 

3, gives a picture, including the twinning of the two students, workplace, computer, may include other options to express pair programming objects or scenes work experience.

 

工作地点:计算机楼410室

计算机:实验室电脑

照片:

 

 

Guess you like

Origin www.cnblogs.com/wkf-/p/11571777.html