首页 > 解决方案 > 如何将输入单词用作整数

问题描述

所以我试图建立一种方式,让人们可以按照以下规则输入他们想要的标志。

所有标志的费用至少为 35.00 美元。

前五个字母或数字包含在最低费用中;每增加一个字符需要 4 美元。

如果标志是橡木做的,加 20.00 美元。松木不收费。

黑色或白色字符包含在最低费用中;金箔刻字需额外收费 15 美元。

因此,如果有人要获得 8 个带有橡木标志和金色字母的字符,那么总金额应该是 82 美元,但是我的代码似乎只处理我提出的第一个 if 语句,使它只有 47 美元,我可以解决这个问题的唯一方法是如果我改变使用数字而不是实际单词的颜色单词。我想知道是否有办法仍然使用这些单词而不是用数字替换它们

失败的代码:

# Declare and initialize variables here.

Charge = 35.00 
Black = 0.00
White = 0.00
Gold = 15.00
Pine = 0.00
Oak = 20.00
Characters = 0.00
Color = 0.00
Wood = 0.00

numChars = int(input("How many characters would you like on your sign? :"))

color = input("What color would you like your words on your sign to be? Black, White, or Gold :")

woodType = input("What type of wood would you like your sign to be? Pine or Oak :")


# Write assignment and if statements here as appropriate.
if numChars > 5:
    Characters = (numChars - 5) * 4.00 
    if color == Gold:
        Color = 15.00
        if woodType == Oak:
            Wood = 20.00

print("The charge for this sign is $" + str(Charge + Characters + Color + Wood))

使用数字而不是单词的代码:

    # Declare and initialize variables here.

Charge = 35.00 
Black = 0.00
White = 0.00
Gold = 15.00
Pine = 0.00
Oak = 20.00
Characters = 0.00
Color = 0.00
Wood = 0.00

numChars = int(input("How many characters would you like on your sign? :"))

color = int(input("What color would you like your words on your sign to be? Black(1), White(2), or Gold(3) :"))

woodType = int(input("What type of wood would you like your sign to be? Pine(1) or Oak(2) :"))


# Write assignment and if statements here as appropriate.
if numChars > 5:
    Characters = (numChars - 5) * 4.00 
    if color == 3:
        Color = 15.00
        if woodType == 2:
            Wood = 20.00

print("The charge for this sign is $" + str(Charge + Characters + Color + Wood))

标签: pythonif-statementstatements

解决方案


在您的代码中,替换Gold"Gold"和。Oak"Oak"

你看,问题是你正在检查是否colorwoodType等于GoldOak 变量,但是你想检查它们是否等于这些字符串,所以你需要把它们放在引号内。


推荐阅读