Preview

04 - Data Types and User Input

 1. As a beginner we will only focus on the following data types: String, Integer, Float and Boolean. Which of the following is a string value?
a="apple"
b="number"
c=1
d=5
e=True
f=False
g=202.27

  a,b,e and f

  None of them are String

  a and b

  c and d

 2. The following variables have been assigned string values. What is the output when the following statement is executed?
letter1="a"
letter2="bc"
print(letter1+letter2)

  a,bc

  abc, as the "+" will concatenate (join together) two strings.

  bc

  Error as you cannot use the "+" with two strings

 3. The following code asks the user for two test scores and then divides it by 2 to get an average. Can you point out why it will not work correctly?
def testaverage():
	testscore1=input("Enter test score 1:")
	testscore2=input("Enter test score 2")
	average=testscore1+testscore2/2
	print(average)

testaverage()

  Actually, it will work correctly. There are no errors

  Line 4 should have a "two" instead of a 2, right at the end of the statement

  Line 2 and 3, the input must be converted into an integer for the division on Line 4 to work

  Line 2 and 3 should have a double == in place in order for it to work correctly

 4. A company in the UK is creating a database driven website and needs to assign data types to each field. They give the field NAME the data type, STRING, and Address Line 1, STRING as well. What is the most appropriate data type for POSTCODE? (e.g SM1 1RT)

  Integer - because it contains numbers

  Float - because it has spaces

  String - because it is alphanumeric (has both numbers and text)

  Boolean - because it either is a post code or isn't

 5. A parent of six children is creating a simple program to help his kids keep track of their pocket money. His eldest son has a pocket money balance of 23.50 this month. What is the most appropriate (from the given list) data type for the variable 'balance'

  boolean, because you either have a balance or are absolutely broke

  integer, because money is always an integer and you can then perform calculations

  float, because it contains decimal points and you need to be able to perform calculations with it

  string, because it may contain a £ sign

 6. A username generation program looks something like this: (see code). Can you spot the error?
def createusername():
	print("====Generating Username====")
	name=input("Enter First Name:")
	date of birth = input("Enter Date of Birth:")
	age=int(input("Enter your age: #"))
	print(name+ date of birth + age)

createusername()

  Line 2, you cannot have capitals inside of a print statement

  Line 5, the 'int' should be 'integer'

  Line 4, a variable name cannot have a space in it (e.g. date of birth should be dateofbirth)

  Line 8, it should call main() not createusername()

 7. The following code will come up with this error: TypeError: cannot concatenate 'str' and 'int' objects on line 4 in main.py
def main():
  firstname="Joe"
  age=33
  print("Your username could be:"+ firstname+age)
  
main()

  True

  False

 8. In the following code, both the variables 'age' and 'hours' need to be used in a calculation. Which one has been set up to obtain the correct 'data type' of user input? Look at Line 3 and Line 8
def age_and_hours():
     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()

  hours has been set up correctly, but age has not (age should be a string input)

  hours should not be an input

  age has been set up correctly, but hours has not (hours should also be integer or float)

  Both age and hours are incorrectly coded

 9. In the following code, what would happen if a user enters the input "one hundred pounds" for the first question?
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)
pocketmoney()

  It will correctly calculate the amount left, by subtracting 'spent' from one hundred

  ERROR: You cannot write "pounds" you have to use the £ symbol after "one hundred" e.g. "one hundred £"

  ERROR: You cannot enter text when it is asking for integer input

  It will ask you the next question, which is: "how much money have you spent?"

 10. In the following code, what would the output be, if the user enters: 20
def guess():
  guess=int(input("Enter a number between 1 and 10:"))
  print("You have guessed:",guess)
  
guess()

  ERROR: You can only select between 1 and 10

  You have guessed: 2

  You have guessed: 20

  You have guessed: 10