简体   繁体   中英

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

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) + ".")

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM