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
Interactive Programs
Interactive Programs
name = input(prompt)
user_name = input("What is your name? ")
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:
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
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:
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? #
#
###
#####
#######
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:
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) !
Casting
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?
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?
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:
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!
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:
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'
Input Checking