ValueError:基数为10的int()的无效文字:''在要求输入时

时间:2018-06-14 19:03:28

标签: python python-3.x

为课程编写示例程序。继续收到此错误:

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

这是包含错误的块。程序运行完全正常,但学校的测试软件失败了。我做错了什么?

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

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,你可能完全没有任何价值地进入:

试试这个:

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

这样做很简单: 它“尝试”运行代码,但如果它出现错误,他将再次请求输入,并打印由于while循环而在except中的任何内容。 每当它做正确的事情时它就会中断。 祝你好运!

修改

BTW,请考虑使用charge += 2,而不是charge = charge + 2。它只是一个捷径,但使代码更清晰。