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.

@mtnzorro

Copy link
Copy Markdown
import random
x = 0 + "babygurl"
  File "dc_exercise_guess_num.py", line 2, in <module>
    x = 0 + "babygurl"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

@AutumnColeman

Copy link
Copy Markdown

First part of the tip_calc2.py code that I messed up:

total = float(raw_input("Total bill amount? "))
service = raw_input(("Level of service?  Good, fair, or bad? " )).low()
guests = float(raw_input("Split how many ways? "))

Spits this:

Total bill amount? 34
Level of service?  Good, fair, or bad? bad
Traceback (most recent call last):
  File "tip_calc2.py", line 2, in <module>
    service = raw_input(("Level of service?  Good, fair, or bad? " )).low()
AttributeError: 'str' object has no attribute 'low'

@AutumnColeman

Copy link
Copy Markdown

This code:

name = int("What is your name?")
print "Hello, %s!" / name

This error:

$ python hello.py
Traceback (most recent call last):
  File "hello.py", line 1, in <module>
    name = int("What is your name?")
ValueError: invalid literal for int() with base 10: 'What is your name?'

@Daz4ever

Copy link
Copy Markdown

def guessingGame():
    my_random_number = random.randint(1, 20)

guess = 5

while guess > 0:

    guess =- 1
    myNum = int(raw_input("Whats the number?: "))
    if myNum == my_random_number:
        print "Yes! You win!"
        break
    if myNum > my_random_number:
        print "%d is too high" % myNum
        print "You have %d guesses left" % guess

    else:
        print "%d is too low" % myNum
        print "You have %d guesses left" % guess

print "You're out of guesses!"
print "Would you like to play again (Y or N)? "

again = raw_input().upper()

if again == "Y":
guessingGame()
else:
print "Bye!"


guessingGame()

output:


You have -1 guesses left
-bash: You: command not found
$ You're out of guesses!

> Would you like to play again (Y or N)? 
> 
> ```
> ```

@ekeyur

ekeyur commented Sep 23, 2016

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."))
if day_of_week == 0 or day_of_week == 6:
    print "It is %d, sleep in." % week day_of_week]
else:
    print "It is %f, go to work." % week[day_of_week]

Produces this error:

$ python day_of_week.py 
  File "day_of_week.py", line 4
    print "It is %d, sleep in." % week day_of_week]
                                                 ^
SyntaxError: invalid syntax

@jesslynlandgren

jesslynlandgren commented Sep 23, 2016

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 a day of the week (number) is entered

>python errortest.py
    Please give a day of week (0-6) 0 is Sunday.0
    Traceback (most recent call last):
         File "errortest.py", line 4, in <module>
             print "It is %s." % week(day_of_week)
        TypeError: 'list' object is not callable

@mccleary

Copy link
Copy Markdown
unpacking argv
from sys improt argv

script, first, second, third = argv

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third



$ python ex13.py
Traceback (most recent call last):
  File "ex13.py", line 3, in <module>
    script, first, second, third = argv
ValueError: need more than 1 value to unpack

@AutumnColeman

Copy link
Copy Markdown

This:

print "Please fill in the blanks below:"
print "____(name)____'s favorite subject in school is ____(subject)____."
name = raw_input("What is name? ")
subject = raw_input("What is subject? ")
print "%s's favorite subject in school is %s.'' % (name, subject)

That:

$ python madlib.py 
  File "madlib.py", line 5
    print "%s's favorite subject in school is %s.'' % (name, subject)
                                                                    ^
SyntaxError: EOL while scanning string literal

@jcortes0309

Copy link
Copy Markdown
# Tip calculator 2

bill = float(raw_input("What is the total bill amount? "))
service = raw_input("What is the level of service? (good, fair, or bad) ")
split = int(raw_input("Split how many ways? "))
good = .20
fair = .15
bad = .10

if service == "good":
    tip = good * bill
elif service == "fair":
    tip = fair * bill
elif service == "bad":
    tip = bad * bill
else:
    print "Try again later and choose a level of service between good, fair and bad"

print "Tip amount is: $.2f" % tip
print "Total amount is: $%.2f" % (tip + bill)
print "Amount per person is: $%.2f" % ((tip + bill) / split)

Gets this error:

python_ex1\ $ python tip_calc2.py 
What is the total bill amount? 100
What is the level of service? (good, fair, or bad) good
Split how many ways? 2
Traceback (most recent call last):
  File "tip_calc2.py", line 17, in <module>
    print "Tip amount is: $.2f" % tip
TypeError: not all arguments converted during string formatting

@benworkman

Copy link
Copy Markdown

File "day_of_the_week.py", line 1
week = raw + 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
^
SyntaxError: invalid syntax

@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