Difference between revisions of "Conversions - Python"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Now Try)
(Data Type Conversions)
 
(One intermediate revision by the same user not shown)
Line 34: Line 34:
 
euros = pounds * rate
 
euros = pounds * rate
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
==Others==
 
==Others==
Line 42: Line 41:
  
 
=Now Try=
 
=Now Try=
'''1)''' Write a program that asks the following questions:===
+
'''1)''' Write a program that asks the following questions:
  
 
#How many children are there?
 
#How many children are there?

Latest revision as of 10:28, 4 March 2019

Data Type Conversions

Convert to Integer

Python has built in functions to convert between different data types. To convert something to an integer you would enter:

number = int( textEntered)

or if you wanted to get the input direct from the user you could do this instead:

name = int( input(enter a number) )

Convert to String

In the previous section Variables you needed to convert the values entered by the user to an integer in this way. You may also need to convert in the opposite way:

text = str( number)

or if you wanted to convert and display the number you could do this instead:

print("the number entered was: " + str(number) )

Convert to Float

In the previous section Variables you needed to calculate how many Euros you would get for a given number of Pounds and an exchange rate. This would be more accurate using a float instead of an integer:

pounds = float( input("Enter the amount in Pounds: ") )
rate = float( input("Enter the exchange rate: ") )
euros = pounds * rate

Others

Other conversion functions are available:

Conversions in python.png

Now Try

1) Write a program that asks the following questions:

  1. How many children are there?
  2. How many sweets do they each have?
  3. How many ducks were there?
  4. How many sweets did each child give each duck?

It should then write a “thrilling” story:

There were [Number of children] children each with a bag containing [Number of Sweets] sweets.  They walked past [Number of ducks] ducks.
Each child gave [Number of sweets each child gives each duck] sweets to each of the ducks and ate one themself. They decided to put the rest into a pile.
They counted the pile and found it contained [Number of sweets left] sweets.

HINT: you will need to convert the input to an integer for each input. eg children = int( input("Enter the number of children") )
HINT: you will need to convert the numerical output to a string. eg print( str(sweets) )

2) Create a program to convert Pounds Stirling into Euros.

  1. It should ask the user to input the amount in Pounds to convert.
  2. It should use the exchange rate of 1.17 Euros to 1 Pound.
  3. It should display the message:
X Pounds Stirling is Y Euros, at the exchange rate of 1.17 Euros to the Pound

HINT: you will need to convert the numerical input to a float. eg pounds = float( input("Enter the number of Pounds") )
HINT: you will need to convert the numerical output to a string. eg print("Euros: " + str(euros) )

3) Now create a version of the above program to allow the user to also input the exchange rate to use.