Python - Text Mode
Although it's great for learning professional programmers would not use Python turtle. They would use a text editor to write a console program ( one that just uses text). Here are some of the basic commands.
Display Text
The print command will display text:
print ("hello")
If you want to display text next to the previous text add end="") onto your prints,
print("These ", end="")
print("words ", end="")
print("are ", end="")
print("in one line ", end="")
Output: "These words are in one line"
print ("hello")
If you want to display text next to the previous text add end="") onto your prints,
print("These ", end="")
print("words ", end="")
print("are ", end="")
print("in one line ", end="")
Output: "These words are in one line"
Get Text from the User
Variables are used to store data. In the code below the variable username will store the name typed in by the user. To display data from the variable, type the variable name.
username = input("Enter username:")
print("Username is: " + username)
username = input("Enter username:")
print("Username is: " + username)
Using IF
- Use IF to check for a certain value. Look at the example code below. When the user types in their order, it is stored in the variable called order.
- On line 10 IF is used to check if the order contains the value 1. If it does then the user ordered a cheese burger. So a message is displayed saying "You have ordered a cheese burger".
- If the user types in 2 then 2 is stored in the variable called order.
- On line 13 IF is used to check if order contains 2. If it does the message "You have ordered a hot dog" is displayed.
Calculating Total Price
Since we already have variables storing the price and the quantity we are ready to calculate the total price. The code below multiples price by quantity and stores the result in the variable total.
- Anything typed into the program is stored as text. So quantity must be cast ( converted) into a number first.
- Since total is a number we have to cast it to a text before it can be displayed.
- Note: if you are interested casting is changing the data type by adding a data type and round brackets. You can find out more here: Python Casting (w3schools.com)
The following code will add two numbers together and display the answer:
print(10 + 5)
The code above only works for the numbers 10 and 5. We call this hard coded because the numbers are always the same. It is better to store the numbers in variables so that they can be changed. The following code will add and then display the two numbers stored in the variables a and b.
a = 3
b = 5
print(a + b)
You could also store the result ( 8 in this example) into a separate variable:
a = 3
b = 5
result = a + b
print( result)
print(10 + 5)
The code above only works for the numbers 10 and 5. We call this hard coded because the numbers are always the same. It is better to store the numbers in variables so that they can be changed. The following code will add and then display the two numbers stored in the variables a and b.
a = 3
b = 5
print(a + b)
You could also store the result ( 8 in this example) into a separate variable:
a = 3
b = 5
result = a + b
print( result)
Calculations - Using Operators
Visual Studio
Use Visual Studio to write your code. It will be easier and you are less likely to lose your code because you can save your projects to One Drive or Google Drive.
I don't have Visual Studio! Here are the steps to install it.
1. Click here to install.
I have installed but I don't have a project! Here is how to create a new project.
1. To open Visual Studio press the Windows button and start typing "Visual Studio" until you see the program with a purple logo. Then press open.
2. If it asks you to login select Skip for Now.
2. If it asks you to login select Skip for Now.
2. Select Create a new project.
3. Search for Python. Then select Python Application
( If you don't see Python Application go to Downloads. Click on the download and select Modify make sure you check the Python Development option. Then press the button in the bottom right which is likely to say Modify Install).
4. Name your project. But don’t include any punctuation!
5. Press the button with three dots to CHANGE THE LOCATION to your OneDrive / Google Drive folder.
5. Press the button with three dots to CHANGE THE LOCATION to your OneDrive / Google Drive folder.
6. Copy and paste your code into the file. It will show lots of errors because your print commands ( e.g. print 'hello' ) have to be changed to use brackets.
For example: Your code: print 'hello' must be: print('hello') The easiest way to start changing this is to search and replace. Press Ctrl H. Search for print ' and replace with print('. This will change the beginning brackets. Then you will have to add each of the closing brackets. |
7. Press the Start button ( with a green triangle ) to run your code. This will automatically save your code.