Python标牌计费程序报错:无法确定颜色类型(Call与str类型不匹配)
标牌费用计算程序错误排查与修复
我在做一个标牌费用计算的编程作业,代码运行结果符合预期(比如金字、8字符、松木材质的标牌最终能算出62美元),但每次测试都会触发错误提示:
Reason: Unable to determine color type. Type mismatch: Cannot replace Call with str.
我的代码
# Initialize variables here. charge = 0 num_chars = 8 color = "gold" wood_type = "oak" minChars = 5 costChars = 4 # Charge for this sign. # Number of characters. print("The first five letters are included in the minimum charge, but each individual character afterwards is an additional 4$") num_chars = int(input("Enter how many characters you will have on the sign: ") if num_chars > minChars: charge = (num_chars - minChars) * costChars # Color of characters. color = input("Do you wan to pay 15$ additional for gold lettering or black and white: ") if color == "gold": charge = charge + 15 elif color == "black and white": charge # Type of wood. wood_type = input("Do you wan to pay 20$ additional oak or pine: ") if wood_type == "oak": charge = charge + 20 elif wood_type == "pine": charge # Write assignment and if statements here as appropriate. # Output Charge for this sign. charge = charge + 35 print(f"The charge for this sign is ${charge:.2f}")
测试失败输出
Status: FAILED! Check: 1 Test: Black and white, 5 letter, on pine (minimum charge). Reason: Unable to determine color type. Type mismatch: Cannot replace Call with str. Error : ValueError - Unable to determine color type. Type mismatch: Cannot replace Call with str. Timestamp: 2024-04-27 20:20:03.443145 Status: FAILED! Check: 2 Test: Gold leaf, 10 letter, on oak. Reason: Unable to determine color type. Type mismatch: Cannot replace Call with str. Error : ValueError - Unable to determine color type. Type mismatch: Cannot replace Call with str. Timestamp: 2024-04-27 20:20:03.443463 Status: FAILED! Check: 3 Test: Gold leaf, 8 letter, on pine. Reason: Unable to determine color type. Type mismatch: Cannot replace Call with str. Error : ValueError - Unable to determine color type. Type mismatch: Cannot replace Call with str. Timestamp: 2024-04-27 20:20:03.443735
问题修复
- 核心语法错误:
num_chars = int(input("Enter how many characters you will have on the sign: "))这一行原代码少了一个闭合括号,导致Python解析代码时出现语法错误,后续的颜色判断逻辑因此失效,触发类型不匹配的报错。 - 拼写错误修正:输入提示里的
wan改为want,避免用户误解输入要求。 - 无效代码清理:
elif color == "black and white":和elif wood_type == "pine":下的单独charge语句无实际作用,可直接删除。 - 输入健壮性优化(可选):对用户输入做去空格和大小写转换,比如
color = input(...).strip().lower(),避免因用户输入大小写或空格导致判断失败。
修复后的完整代码
# Initialize variables here. charge = 0 num_chars = 8 color = "gold" wood_type = "oak" minChars = 5 costChars = 4 # Charge for this sign. # Number of characters. print("The first five letters are included in the minimum charge, but each individual character afterwards is an additional 4$") num_chars = int(input("Enter how many characters you will have on the sign: ")) if num_chars > minChars: charge = (num_chars - minChars) * costChars # Color of characters. color = input("Do you want to pay 15$ additional for gold lettering or black and white: ").strip().lower() if color == "gold": charge += 15 # Type of wood. wood_type = input("Do you want to pay 20$ additional for oak or pine: ").strip().lower() if wood_type == "oak": charge += 20 # Output Charge for this sign. charge += 35 print(f"The charge for this sign is ${charge:.2f}")
内容的提问来源于stack exchange,提问作者liam ruddy
相关产品推荐
相关产品推荐

