Skip to content

Instantly share code, notes, and snippets.

@airportyh
Created September 23, 2016 13:43
Show Gist options
  • Select an option

  • Save airportyh/6a55675f52df04e6c3b24d7ba530a2f4 to your computer and use it in GitHub Desktop.

Select an option

Save airportyh/6a55675f52df04e6c3b24d7ba530a2f4 to your computer and use it in GitHub Desktop.
Catalog of Python Errors

Enter a python error message, and the example code that caused that error.

@mwdowns

mwdowns commented Sep 23, 2016

Copy link
Copy Markdown
bill = float(raw_input("How much was the bill? "))
service = str.upper(raw_input("How was the service (Good, Bad, OK)? "))
customer = float(raw_input("Split how many ways? "))

if service == "GOOD":
    print "Tip amount is $%.2f."  ((bill * .20))
    print "Total is $%.2f." % ((bill + (bill * .20)))
    print "Amount per person: $%.2f." % ((((bill + (bill * .20))) / customer))

elif service == "BAD":
    print "Tip amount is $%.2f." % ((bill * .10))
    print "Total is $%.2f." % ((bill + (bill * .10)))
    print "Amount per person: $%.2f." % ((((bill + (bill * .20))) / customer))

else:
    print "Tip amount is $%.2f." % ((bill * .15))
    print "Total is $%.2f." % ((bill + (bill * .15)))
    print "Amount per person: $%.2f." % ((((bill + (bill * .20))) / customer))

My convoluted code, missing the % in the first line of the if print statement, returns this error:

 python tip_calc2.py 
How much was the bill? 20
How was the service (Good, Bad, OK)? good
Split how many ways? 5
Traceback (most recent call last):
  File "tip_calc2.py", line 6, in <module>
    print "Tip amount is $%.2f."  ((bill * .20))
TypeError: 'str' object is not callable

@Pumala

Pumala commented Sep 23, 2016

Copy link
Copy Markdown
name = raw_input("what is your name? ".upper()).upper()
name_length = len(name)
print "HELLO %s YOUR NAME HAS %d LETTERS IN IT." % name, name_length

Gets this error:

~/Downloads/student-solutions: python hello2.py 
WHAT IS YOUR NAME? we
Traceback (most recent call last):
  File "hello2.py", line 3, in <module>
    print "HELLO %s YOUR NAME HAS %d LETTERS IN IT." % name, name_length
TypeError: not enough arguments for format string

@jesslynlandgren

Copy link
Copy Markdown
week = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

day_of_week = int(raw_input("Please give a day of week (0-6) 0 is Sunday.  "))
print "It is %s." % week[day_of_week]

produces this error when an invalid day of the week is entered

python errortest.py
Please give a day of week (0-6) 0 is Sunday.  7
Traceback (most recent call last):
  File "errortest.py", line 4, in <module>
    print "It is %s." % week[day_of_week]
IndexError: list index out of range

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment