1 of 17

CSc 110 - Summer 2017

Introduction to Programming I

Lecture 5: User Input

Benjamin Dicken

Adapted from slides by Allison Obourn, Marty Stepp, and Stuart Reges

2 of 17

Interactive Programs

  • Interactive program: Reads input from the console, and uses the input value in the program
    • While the program runs, it asks the user to type input
    • The input typed by the user is stored in variables in the code
  • Can be tricky; users are unpredictable and misbehave!

3 of 17

Interactive Programs

  • input: A function that reads input from the user in the python console
    • Waits for user to type, and then the user hits “Enter” to let the program read in the characters and continue running
  • input: returns a value of type string
    • (More on the concept of “return” later)
  • Using the input function to read console input:

name = input(prompt)

  • Example:

user_name = input("What is your name? ")

  • The string the user types in (followed by “Enter”) is assigned to the variable user_name

4 of 17

Input example 1

def main():

first_name = input("What is your first name? ")

last_name = input("What is your last name? ")

print("Your full name is " + first_name + " " + last_name)

The result of running this in the python console would be something like:

5 of 17

Input example 1

def main():

first_name = input("What is your first name? ")

last_name = input("What is your last name? ")

print("Your full name is " + first_name + " " + last_name)

The result of running this in the python console would be something like:

>>> What is your first name? Ben

>>> What is your last name? Dicken

>>> Your full name is Ben Dicken

6 of 17

Input example 2

def main():

c = input("What character to use for triangle? ")

print(" " + c + " ")

print(" " + (c*3) + " ")

print(" " + (c*5) + " ")

print( (c*7) )

The result of running this in the python console would be something like:

7 of 17

Input example 2

def main():

c = input("What character to use for triangle? ")

print(" " + c + " ")

print(" " + (c*3) + " ")

print(" " + (c*5) + " ")

print( (c*7) )

The result of running this in the python console would be something like:

>>> What character to use for triangle? #

#

###

#####

#######

8 of 17

Input example 3

def main():

age = input("How old are you? ")

years = 65 - age

print(years + " years until retirement!")

The result of running this in the python console would be something like:

9 of 17

Input example 3

def main():

age = input("How old are you? ")

years = 65 - age

print(years + " years until retirement!")

The result of running this in the python console would be something like:

>>> How old are you? 38

Traceback (most recent call last):

File "<pyshell#13>", line 1, in <module>

print(65 - age)

TypeError: unsupported operand type(s) for -: 'int' and 'str'

...Because age is a string, not a number (int, float) !

10 of 17

Casting

  • We often need to convert a variable or value of one type to another type
  • This can be accomplished with casting
  • Casting: the process of converting a value (possibly stored in a variable) of one type to a different type
  • Not all types can be cast to all other types, but many can!
  • Use python built-in casting functions to do the conversion
    • int(X) converts X to an integer
    • float(X) converts X to a float
    • str(X) converts X to a string
    • bool(X) converts X to a boolean

11 of 17

Casting Example 1

pi = 3.14 # pi is a float

print( str(pi) ) # convert pi to string, then print

pint = int(pi) # convert pi to an int

print( str(pint) ) # what does this print?

pi2 = float(pint) * 2.0 # convert pint to float, multiply by 2

print( str(pi2) ) # ???

What will this program print out?

12 of 17

Casting Example 2

first_name = "benjamin"

year_born = "1974"

print( year_born ) # what will this print?

year_born_num = int(year_born) # will this work?

print( str(year_born_num) ) # what will this print?

name_num = float(first_name) # will this work?

print( str(name_num)) # what will this print?

13 of 17

Input example 3 - b

def main():

age = int(input("How old are you? "))

years = 65 - age

print(years + " years until retirement!")

The result of running this in the python console would be something like:

14 of 17

Input example 3 - b

def main():

age = int(input("How old are you? "))

years = 65 - age

print(years + " years until retirement!")

The result of running this in the python console would be something like:

>>> How old are you? 29

>>> 36 years until retirement!

...Because now we are casting properly!

15 of 17

Input example 3 - c

def main():

age = int(input("How old are you? "))

years = 65 - age

print(years + " years until retirement!")

The result of running this in the python console would be something like:

16 of 17

Input example 3 - c

def main():

age = int(input("How old are you? "))

years = 65 - age

print(years + " years until retirement!")

The result of running this in the python console would be something like:

>>> How old are you? BLAH BLAH BLUH

Traceback (most recent call last):

File "test.py", line 6, in <module>

main()

File "test.py", line 2, in main

age = int(input("How old are you? "))

ValueError: invalid literal for int() with base 10: 'BLAH BLAH BLUH'

17 of 17

Input Checking

  • Casting helps, but what if users don’t “follow the rules” and input the types of values that the program expects?
  • We need a way to check that the values being provided usable
  • How?
  • Onto the next slide deck!