1

Writing sample program for class. Keep getting this error:

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

on line 1.

Here is the block containing the error. Program runs completely fine, but testing software for school is failing it with this. What am I doing wrong?

"""
HouseSign.py - This program calculates prices for custom house signs.
"""

# Declare and initialize variables here.
    # Charge for this sign.
    # Number of characters.
    # Color of characters.
    # Type of wood.
charge = 0
numChars = int(input("How many letters do you want? "))
color = input("What color letters do you want? ")
woodType = input("What type of wood do you want? ")

if numChars < 5:
    charge = charge + 0
else:
    charge = charge + 0
if numChars >= 6:
    charge = (numChars - 5 ) * 4
else:
    charge = charge + 0

if color=="gold":
    charge = charge + 15
else:
    charge = charge + 0
if woodType=="oak":
    charge = charge + 20
else:
    charge = charge + 0

charge = charge + 35  
# Write assignment and if statements here as appropriate.

# Output Charge for this sign.
print("The charge for this sign is $" + str(charge) + ".")
Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
  • the program does _not_ run "completely fine." try something as input that is not a number, like `x` –  Jun 14 '18 at 19:07
  • `''` means you hit ENTER when asked for input, it's the empty string. Perhaps you were supposed to handle incorrect entries too? – Martijn Pieters Jun 14 '18 at 19:08
  • Possible duplicate of [ValueError: invalid literal for int() with base 10: ''](https://stackoverflow.com/questions/1841565/valueerror-invalid-literal-for-int-with-base-10) –  Jun 14 '18 at 19:21
  • Ok. Understood. So, could I just get that to run as a string instead of assuming the person using it is going to enter an integer when the question is asking for a number? – Herbert Showalter Jun 14 '18 at 19:36
  • If you do that, your program will crash later. Try it.You might want to have a look at this question and the answers: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Jun 14 '18 at 19:49

1 Answers1

0

As mentioned in the comments, you're probably ENTERING without any value at all:

try this:

while True:
    try:
       numChars = int(input("How many letters do you want? "))
       break
    except:
       print("Oops, make sure you're entering a valid value")
while True:
    try:
       color = input("What color letters do you want? ")
       break
    except:
       print("Oops, make sure you're entering a valid value")
while True:
    try:
       woodType = input("What type of wood do you want? ")
       break
    except:
       print("Oops, make sure you're entering a valid value")

What this does is simple: It "tries" to run the code, but if It gets an error, he will ask for the input again, and print whatever is in the except, because of the while loop. It breaks everytime it does the correct thing. Good Luck!

EDIT:

BTW, consider using charge += 2, instead of charge = charge + 2. It's just a shortcut, but makes the code cleaner.

Duarte Arribas
  • 317
  • 3
  • 10