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
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 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

2 Answers2

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
-1

It's important to follow the proper coding structure when writing your blocks. You should always start with your declarations & housekeeping, then write out the main function, then finally your output.

# Declare and initialize variables here.
# Charge for this sign.
charge = 0.00
# Number of characters.
numChars = 8
# Color of characters.
color = "gold"
# Type of wood.
woodType = "oak"

# First, our charge is declared to be a 
# minimum of $35 for any sign made.
charge = 35.00

# If our sign has more than 5 characters, there is a
# $4 fee for each character after 5.
if numChars > 5:
    charge += (numChars - 5) * 4.00

# If sign is made of oak, there's a $20 upcharge.
if woodType == "oak":
    charge += 20.00
# If sign is made of pine, no additional charge.
elif woodType == "pine":
    charge += 0

# Gold-Leaf lettering is a $15 upcharge.
if color == "gold":
    charge += 15.00
# Black and White lettering included in
# minimum charge.
elif color == "black" or "white":
    charge += 0



# Output Charge for this sign.
print("The charge for this sign is $" + str(charge))
  • While the stated advice is generally good, this post is **in no way** an answer to the question. Also, you are just ignoring the inputs and the overall intent of the OP. Moreover, it is very much debatable, if you **actually improved** the code with all your comments. In a language such as Python, choosing descriptive names often makes individual code passages self-explanatory, since they read like pseudo-code anyway. Your comments just re-state every line of code in English. **That is not good coding style.** What's the benefit of e.g. `# Type of wood.` above the line `woodType = "oak"`? – Daniil Fajnberg Aug 31 '22 at 11:32
  • Hello, Daniil? Are you asking why I am using comments for my solution? As stated in the original request, the program is being prepared by a student, so certain comments might be placed in the solution in the spirit of oversimplification. Also, can you please explain what you think the poster's problem is? Thank you. – cloudflips32 Sep 03 '22 at 06:16