Job 1+2. Arithmetic (improved full version, written in Python) _064121 Vanguard Composire Technique

Source: Internet
Author: User

Overview:

With one weeks plus 51 of the three-day vacation self-taught Python, on Mac system to re-write the arithmetic program, the compiler is pycharm, equivalent to complete the job 2.d) "Choose a programming language you have never learned, try to achieve the basic function" in this direction.

This program, written in Python, can achieve the addition and subtraction of fractional programs, the implementation of the calculation with subtraction and parentheses, the realization of randomly generated 100 simple calculation questions, than previously written in C + + program functions more perfect, more concise!

Key points:

Call two templates.

1) similar to C + + and Java, the format is an object. Method (), fraction () method, which can directly represent fractions.

2) Random.randint (A, b) is used to generate a random integer within a a-range.

p=["+", "-", "*", "/";

Random.choice (P)

P represents an array of characters, and Random.choice (p) indicates that the characters are randomly extracted from the array p!

Process:

1)

  

The function of this code is to realize the subtraction of two fractions, and realize the user input and judge the right and wrong, and finally count the number of correct and error!

Use X,y,a,b to represent the numerator of the first fraction, the denominator of the first fraction, the numerator of the second fraction, the denominator of the second fraction!

M,n represents the user-entered answer, M is a molecule, n is the denominator, and then the IF Else statement is used to determine the right or wrong.

H,i is the counter, recording the correct number of errors, respectively, the console operation results are as follows:

1/2 and 1/3 were entered respectively, the first three operations were correct, showing "congratulations!! Your answer is right! "

Fourth arithmetic error, showing "the right answer of ' (x/y)/(A/b) IS:3/2 '"

Finally, the number of correct answers is 3, the number of error answers is 1.

2)----------------------------------------------------------------------------------------------------------------------- --------

Produces 10 random variables, ranging from 1 to 20, expressed in a1-a10, respectively.

Then generate 3 different types of calculation questions, and let the user enter the answer, machine judgment right and wrong, console results are as follows:

The answer to question 1 is 64, and I answered it correctly.

The answers to questions 2 and 3 are 1616/741,-4039/78, I answer the wrong, not careful to calculate, more complex, casually filled out two answers!

3)------------------------------------------------------------------------------------------------------------- ----

The last link is level 3, randomly generating 100 calculation questions.

First, the array is used to store the subtraction symbol, then the random number _ symbol _ random number in print to express the calculation.

In the calculation plus while loop, the counter count initial value is set to 1, loop 100 times, generate 100 equations, the result is as follows:

A total of 100 question, number random, operator random.

Summarize:

1) This program is written by myself, because the Pycharm compiler can no longer characters and strings appear in Chinese, comments can not be used in Chinese, so only in the blog typing comments, functionally more than the first write the perfect a lot! This also benefits from Python is an explanatory language, the syntax is very simple: for example, two fractions of arithmetic this function, using C + + needs to use operation+, overloading and other functions, code lines of nearly 100 lines, but only 9 lines of Python can be done!

2) This procedure has a flaw: your request on the PPT is to generate the 100 calculation questions can not be duplicated, for example (1+2) and (2+1) is the repetition of the topic, and I write with Python did not realize this detail, after all, only 1 weeks reading books, there are many details did not learn in place, is still thinking, If you want to make a change before May 7, send it to the blog!

3) Through this one-week Python self-study, a lot of harvest, especially by its simple grammar shock, this program I believe can also be expressed in a more concise way.

Compared with C + +, C, Python is more concise, do not need to consider the underlying factors, such as pointers, memory consumption, such as the release of complex things, but conversely, in the operational efficiency, Python is not as C, C + +. But think, in the ordinary road, taxi and F1 racing together, speed will not be too big difference, the difference between the two can only be reflected on the track. For our students, it is very rare to write oversized programs, so Python is ideal as an entry-level programming language and easy to get started!!

4) Cnblogs seemingly can not upload files, Python code I will be in the May 7 before the other homework package sent to teacher fan qq/mailbox, you can run the test!

Job 1+2. Arithmetic (improved full version, written in Python) _064121 Vanguard Composire Technique

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.