Programming with Python 1
In this part of our Python tutorial, we will program a math game in
which the program offers randomly simple calculation tasks to the user. The
user of the program can answer it either wrong or correct. The program will
output the number of attempts in the console after the calculation.
So that we can write the code flawlessly, we need some basics that we have to repeat as we learned in the Java programming language.
Variables and operators
To store values variables are needed. Variables in Python, as well as in all programming languages, serve as placeholders as in mathematics. Operators are used to perform calculations.
Calculation and assignment
The following program performs a simple calculation using an operator. The result of the calculation is then output in the console.
As we can see, the program can do more than just add but can do all four basic operations. We store the values for the variables a and b and calculate the addition, subtraction, multiplication and division of the two variables. The results are then output to the console.
Enter a string
Of course, a console application is boring if the user can not connect directly to the program. So once the program has been started, the user has no influence on the program flow. In order to change this, we will write a program in which the program waits for the input of the user and finally executes with this the processing of the information.
So that we can write the code flawlessly, we need some basics that we have to repeat as we learned in the Java programming language.
Variables and operators
To store values variables are needed. Variables in Python, as well as in all programming languages, serve as placeholders as in mathematics. Operators are used to perform calculations.
Calculation and assignment
The following program performs a simple calculation using an operator. The result of the calculation is then output in the console.
We can also write it as a program and secure it. Of
course, the program would only serve for a specific bill.
The output of the program is as follows:
As we can see, the program can do more than just add but can do all four basic operations. We store the values for the variables a and b and calculate the addition, subtraction, multiplication and division of the two variables. The results are then output to the console.
Enter a string
Of course, a console application is boring if the user can not connect directly to the program. So once the program has been started, the user has no influence on the program flow. In order to change this, we will write a program in which the program waits for the input of the user and finally executes with this the processing of the information.
The program asks the user for his name. No matter what
the user enters, it is saved as a character. If the user types in letters or
numbers, the program saves them as a string and can currently do nothing more
than just output them. We assume that the user can not confuse his name with
any number and type in it correctly. So the name of the user is stored as long
as the program is executed in the memory.
Since after the output of the input, which was operated by the user, is executed, the program is terminated and the information is deleted from the working memory.
Since after the output of the input, which was operated by the user, is executed, the program is terminated and the information is deleted from the working memory.
Enter a number
Entering a number is similar to typing a character, but the program must prompt the program to store the numbers as a number, not as a string. To do this, the programmer must enter the type of data type in which the number should be stored before the input command. If it is a rational number, the user can apply the integer data type. If the input is a real number, then the user must apply the float data type. Since we already see the advantages of the programming language Python. We have only two primitive data types available. For this part, here is an example:
Entering a number is similar to typing a character, but the program must prompt the program to store the numbers as a number, not as a string. To do this, the programmer must enter the type of data type in which the number should be stored before the input command. If it is a rational number, the user can apply the integer data type. If the input is a real number, then the user must apply the float data type. Since we already see the advantages of the programming language Python. We have only two primitive data types available. For this part, here is an example:
Now that we know the skills needed to program our game,
we can get down to business and program the game.
The little game gives the user a task. After entering the
solution from the user, the program reveals the right solution.
Tasks
Write a program that converts any inch values to centimeters and outputs them. 1 inch = 2.54 cm
Write a program to calculate the monthly tax payable. The user is prompted to enter his monthly gross salary. Subsequently, 19% of this contribution will be calculated and spent.
Tasks
Write a program that converts any inch values to centimeters and outputs them. 1 inch = 2.54 cm
Write a program to calculate the monthly tax payable. The user is prompted to enter his monthly gross salary. Subsequently, 19% of this contribution will be calculated and spent.
Kommentare
Kommentar veröffentlichen