Write a program which selects two integer numbers randomly, adds the numbers and asks the user to enter the answer and then checks if the answer is correct or not
Answers
Answer:
Inputting & Outputting Data in Python
Outputting Data
If you want to display something on screen you can use the print() function. An example of code that prints to screen in IDLE is shown below:
print("Hello World")
This is what it would look like when run in IDLE:
Outputting1
Inputting Data
If you want the user to enter data into the program, you can use the input() function. An example of code that will ask the user to enter their name and display it on screen using print() is shown below:
name = input("What is your name? ") # displays the message on screen and stores the input from the user in a variable called name
print("Hi " + name)
This is what it would look like when run in IDLE:
Outputting2
Example program 1 - Enter a Word
The code for the program below will allow the user to enter a word and store it in a variable called word. It will then use the print() function to output the word that they entered.
word = input("Please enter a word ")
print("You entered the word " + word)
When run in IDLE:
Outputting3
Example program 2 - Address Program
The code for the program below will allow the user to enter various pieces of information and store them in different variables. The print() function is then used to output all of the information.
number = input("Enter your house number: ")
street = input("Enter your street name: ")
town = input("Enter your town/city: ")
county = input("Enter your county: ")
postcode = input("Enter your postcode: ")
print("\nAddress Details:\n" + "Street: " + number + " " + street + "\nTown/City: " + town + "\nCounty: " + county + "\nPostcode: " + postcode)
When run in IDLE:
Outputting4
You can concatenate (join together) variables with strings in a print() function. In the address example print("Street: " + number + " " + street + "\nTown/City: " + town) will combine the strings “Street” and “Town/City” with the variables number, street and town.
\n is used to start a new line when it is displayed on screen.
Variables
A variable is used to temporarily store a piece of data.
For example:
number1 = 10
In the code above the variable is called number1 and the value it is storing is 10. Variables can hold any type of data. Using variables makes it easier for people to understand what is going on.
For example:
cost = 15
VAT = 3
total_cost = cost + VAT
Casting Variables
Python will automatically decide what type of data a variable should be, sometimes this isn’t right, therefore you will have to convert/cast variables to a different data type.
Integer
The program below will multiply a number by 5. When data is input from the user it will store it as a string. You will need to convert the variable number to an integer before performing a calculation. An example of how you do this is shown below:
number = input("Enter a whole number ")
answer = int(number) * 5 #converts the variable number to an integer and multiplies it by 5.
print(answer)
Real / Float
The program below will ask the user to enter their weight in kilograms (this could be a decimal) and convert it to pounds. You will need to convert the variable kg to a float before converting it. An example of how you do this is shown below:
kg = input("Enter the weight in KG ")
pounds = float(kg) * 2.2
print(pounds)
String
Using the program above, if you wanted to improve the print message so that it said Your weight in pounds is X you would need to convert the variable pounds to a string as it is currently a float.
print("Your weight in pounds is " + pounds)
This is what would happen if you ran the code currently:
Variables1
This is because the variable pounds is a float and to be joined with a string in the print message it needs to be converted to a string, the code is below:
print("Your weight in pounds is " + str(pounds))
Now when run the code it will work correctly:
Variables2
Example program 1 - Water Tank Capacity Program
The code for the program below will allow the user to enter the height, width and depth of a water tank, then calculate and output the capacity.