Friday 26 September 2014

7. Input Output



Learning outcomes
You will learn to use:
The input function
The int function
String formatting
Number formatting


Activity 6.1
Input function
The input function allows the user to input a value and assign it to a variable.
Copy and run this program.
myAnswer=input("Please enter your name: ")
print(myAnswer)

Re-run the program several times, entering different names each time.

Write a program that asks the user for their name and favourite food and displays these on the screen.
Activity 6.2
Int function
Copy and run this program.
age=input("Please enter your age: ")
agePlusTen = age + 10
print("You will be",agePlusTen,"in 10 years")

Explain why it does not work.
Correct the program.
Activity 6.3
Write a program that asks you to enter a number then displays the number doubled.
Activity 6.4
String formatting
The string method .format gives you more control over the formatting of your output than printing using space-separated values. The string formatting commands are given in curly brackets {}.

Copy and run these lines of code:

>>>foodOne="fish"
>>>foodTwo="chips"
>>>print("{0} and {1}".format(foodOne,foodTwo))
fish and chips

>>>print("{1} and {0}".format(foodOne,foodTwo))
chips and fish

>>>print("{1} {1} {1}  and {0} {0} {0}".format(foodOne,foodTwo))
chipschipschips and fishfishfish

Create these variables:
one = “cheese”
two =”onion”

Use the .formatcommand to display:

My favourite crisps are cheese and onion. I love them!
cheese and onion and cheese and onion and cheese and onion
cheesecheesecheese and oniononiononion
You guessed it. The best crisps are onion and … cheese.

Try altering the flavours assigned to the variables to your favourite flavour! Enjoy.
Activity 6.5
Formatting numbers
You can use the .format method to format the number of decimal places.  

Copy and run these lines of code:

>>>number = 98.1468297645
>>>print("The answer is {0:.5f}".format(number))
The answer is 98.14683

>>>print("The answer is {0:.4f}".format(number))
The answer is 98.1468

>>>print("The answer is {0:.3f}".format(number))
The answer is 98.147

>>>print("The answer is {0:.1f}".format(number))
The answer is 98.1

>>>print("The answer is {0:.0f}".format(number))
The answer is 98

Assign the number 765.87641987 to a variable and display the number with 5, 2 and no decimal places using the .format command.
Activity 6.6
Write a program that asks how much your bill is at a restaurant and then asks you to enter the % you want to give in a tip. The program should display the amount of tip to give to the waiter.

Amend the program so that it also displays the total cost of the meal including the tip.
Activity 6.7
Write a program that displays the square of a number.
Write a program that prompts for a number and then displays the cube of a number.
Write a program to find the perimeter of a square.
Write a program to find the perimeter of a rectangle.
Write a program that finds the area of a square.
Write a program that finds the area of a cube.
Write a program to convert from pounds to euros.

Hint: All these programs should prompt for the input information and display the result appropriately.

No comments:

Post a Comment