繁体   English   中英

ValueError: int() 以 10 为基数的无效文字:'' 询问输入时

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

为 class 编写示例程序。 不断收到此错误:

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

如评论中所述,您可能完全没有任何价值地进入:

尝试这个:

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循环而打印异常中的内容。 每当执行正确的操作时,它就会中断。 祝好运!

编辑

顺便说一句,考虑使用charge += 2 ,而不是charge = charge + 2 这只是一个捷径,但是会使代码更简洁。

在编写块时遵循正确的编码结构很重要。 您应该始终从您的声明和管理开始,然后写出主要的 function,最后是您的 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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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