Back

A menu structure

Q1. Type the following code into Python and get it working:

#A simple menu structure

import math

myNumber = int(input('Please enter any whole number >>> '))
choice = input(
'''
*****************************************************

Good choice! What would you like to do with it?

Type s to find the square of the number >>>
Type root to find the square root >>>
Type fact to find the factorial of the number >>>

and then press <ENTER> .....


''')

if choice == 's':
    print('The square of',myNumber,'is',myNumber*myNumber)
elif choice == 'root':
    print('The square root of',myNumber,'is',math.sqrt(myNumber))
elif choice == 'fact':
    print('The factorial of',myNumber,'is',math.factorial(myNumber))
else:
    print('Not a valid choice. Bye')

Q2. Why do we need to use the statement import math?
Q3. In the line myNumber = int(input('Please enter any whole number >>> ')) why did we use int?
Q4. Why did we need to use the else keyword?
Q5. Find the Python 3 math module documentation. Approximately how many different functions are available?
Q6. What error message would you get if you typed 

    print('The square root of',myNumber,'is',sqrt(myNumber)) 

instead of 

    print('The square root of',myNumber,'is',math.sqrt(myNumber)) 

Q7. Add an extra choice to your above code and test it works.
Q8. Find out how to round the square root so it has only 2 decimal places. Test your code to check that the square root is being rounded to two decimal places.
Q9. Using a function in the math module, print out pi.
Q10. How can we find out the datatype of a variable e.g. the data type of the variable called 'choice'?

Back