~ User Input - taking integer input from a user


5 - Obtaining user input (integers)

User Input - Obtaining string (text) user input

So far we have been declaring variables such as x and name, and giving them values in the program itself. For instance x = 3 or name="Joe". What if we wanted the user to decide the input? That's what normally takes place in programs so that's what we are going to be looking at. When you log in to facebook or instagram, the first thing you do is INPUT your username and password. This sort of input is string input and is distinct from integer (number)input. Let's look, in this session, at obtaining INTEGER input from the user.

The term Integer in Python is simply referring to a whole number (e.g. 3 or 100). When you use the input command, the default is for Python to assume it is string input. In order to work with integers (and do maths), you need to convert it to an integer input. There are two ways of doing this, and we'll look at how to do that in the presentation below. We will also look at how to take a integer input and convert it into a string, if it is necessary to do so!

Download Python 3 here:



Task 1

1.First, run the code to see how it works! This is a function called verify that asks the user for their age and responds based on the integer input of that age. It then goes on to ask theuser how many hours they spend on homework. Again, it responds based on the user input. The if statement uses a comparison operator, and for this to work the input must be an integer.

2. The age integer input works fine, but the hours needs to be converted into an integer input. Can you do this to make the program work?

Code

def age_and_hours():
     #The age integer input works fine, but the hours needs to be converted to integer. Can you do this to make the hours of homework calculation work properly?
     age=int(input("What is your age?:"))
     if age>12:
          print("You are old!")
     else:
          print("Wow...your whole life ahead of you")
     hours=input("Enter the no. of hours you spend on homework per week:")
     if hours>10:
          print("Wow - you do a lot of independent study!")
     else:
          print("...are you sure that's enough to help you get ahead?!")

age_and_hours()

Task 2

1.Run the code below and note how it works..

2. Add another variable and ask for a number input for 'z' (this will give three integer inputs).

3. Change the code slightly so that the total variable adds up the three numbers x + y + z instead of just x + y.

Code

def simplecalc():
     #add another variable and ask for a number input for 'z'
     #make the total variable add up the three numbers x+y+z
     x=input("Enter a number:>>")
     y=input("Enter another number:>>")
     total=int(x)+int(y)
     print("Adding these numbers will give you",total)
simplecalc()

Task 3

1. The program below seeks to add up numbers. Play the program and you'll see that the sum for num1 and num2 doesn't quite work as it should. Can you fix this problem? Use the second set of numbers (num3 and num4) which DO work correctly, to help you solve this problem!

Code

def sum():
     print("This is a program that will perform addition")
     num1=input("Enter Number 1:") #convert this input to integer
     num2=input("Enter Number 2:") #convert this input to integer
     sum=num1+num2 #this sum will not work properly unless conversion to integer is done
     print("Adding the two numbers gives you:",sum)
     num3=int(input("Enter Number 3:"))
     num4=int(input("Enter Number 4:"))
     total=num3+num4
     print(total)
     print("Adding num3 and num4 gives you:",total)
sum()

Task 4

1. This is a simple program that asks the user for their current amount (of pocket money), and also asks them how much they have spent (which is stored in the variable spent). The variable left stores the final amount (which is current pocket money MINUS spent!).

2. Create another variable called 'gift', and ask the user to enter a gift amount. This will be an amount ADDED to their account. Calculate the new value of the variable 'left', and print it to the screen.

Code

def pocketmoney():
     print("Keeping track of your pocket money")
     money=int(input("How much money do you have in the piggy bank?:"))
     spent=int(input("How much money have you spent?:"))
     left=money-spent
     print("You have this much money left:",left)
     #Create another variable called gift, ask the user to enter a gift amount
     #Don't forget to convert the input for gift to integer
     #Calculate the new value of left, and print it to the screen
pocketmoney()

Task 5

1.This simple program just asks for two values of x and y and then seeks to divide them using the '/' divide operator.

2.On running the proram you'll note it does not work and that's because x and y (inputs) need to be converted into integers first. Do this and fix the program!

3. See if you can extend the program by also printing out the result of multiplying x and y

Code

def division():
     print("Subtraction")
     x=input("Enter a number larger than 10:")
     y=input("Enter a number between 1 and 5:")
     z=x-y
     print(z)
     #x and y need to be converted into integers before the calculation will work.==You logged in succesfully!!!!======")
division()

Final Challenge

Use your knowledge and skills from the completed tasks. Use the trinket below to complete your challenge.

Create a simple "===guess the number===" program. Create a function called guess e.g def guess(): Inside this function, ask the user to enter a number between 1 and 10. Store the result of the user's guess in the variable "guess". e.g. guess=input ....etc. Make sure the guess variable is converted to an integer. Print the guess like so: "You have guessed",guess. (remember to use a comma and not a + when printing!)

Note: You can print the page, screenshot, write the code in your book, or show the teacher your page.

Answers

Coming soon!