Preview

06 - User Input Integer

 1. The term _________ in Python (and generally in mathematics) is referring to a whole number. E.g 100, or 3
Note: The following video from www.teachyourselfpython.com provides examples and a full overview of obtaining integer user input

  real

  string

  float

  integer

 2. Analyse the code below and explain why line 4 causes an error.
num1=int(input("Enter number 1:"))
print(num1+2)
num2=input("Enter number 2:")
print(num2+4)

  num2 has not been converted to an integer and as a string it cannot be added to

  There is no error

  num2 is an integer and it should be a string

  num2 should be declared as a boolean value

 3. Here, a programmer has attempted to convert the user input for num2 to an integer, but there is an error. What is it?
num2=int(input("Enter number 2:")
print(num2+4)

  A msising bracket at the end of line 1

  There is nothing missing - this is a mystery

  Speech marks around "num2" in line 2

  A missing colon at the end

 4. There is another way of converting num2 to integer without doing it at the input stage. What is it?
num2=input("Enter number 2:")
print(num2+4)

  Change line 2 to: Cint(num2)+4

  None of the above

  Change line 2 to: print(int(num2)+4)

  Change line 2 to: convert_to_integer(num2) + 4

 5. In the following code, on what line have the inputs been converted into integers?
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()

  On Line 8

  On Line 6

  They have not been converted into integers

  On Line 1

 6. In the following program, if you enter the numbers 8 and 6, what will the output be?
def sum():
     print("This is a program that will perform addition")
     num1=input("Enter Number 1:") 
     num2=input("Enter Number 2:") 
     sum=num1+num2
     print("Adding the two numbers gives you:",sum)
sum()

  14

  86

  56

  Error

 7. The following program does not work. What line could you change to fix this and to what?
def pocketmoney():
     print("Keeping track of your pocket money")
     money=int(input("How much money do you have in the piggy bank?:"))
     spent=(input("How much money have you spent?:"))
     left=money-spent
     print("You have this much money left:",left)
pocketmoney()

  Line 4 to: int(input("How much money have you spent?:")) = spent

  Line 4 to: spent=int(input("How much money have you spent?:"))

  This is a critical error that cannot be fixed by conversion to integer

  Line 3 to: int(money)input("How much money do you have in the piggy bank?:"))

 8. Can you spot the error?
def division():
     print("Subtraction")
     x=input("Enter a number larger than 10:")
     y=input("Enter a number between 1 and 5:")
     z=int(x)-y
     print(z)
division()

  y has been converted to an integer but x has not

  Subtraction cannot be performed on integers - real numbers would be necessary

  x is a number larger than 10 which python does not allow

  x has been converted to an integer but y has not

 9. Whole numbers (numbers with no _______________) are called integers

  decimal place

  numerical values above 100 or under 0.5

  numerical values below 10

  strings

 10. What is the output for the following program if the inputs are 9 and 9
x=input("Enter value for x:")
y=input("Enter value for y:")
print(x+y)

  18

  99

  An error would occur

  77