将整数分配给字符串,然后添加它们

Assigning Integers to strings and then adding them

提问人:twalk126 提问时间:11/16/2023 更新时间:11/16/2023 访问量:35

问:

我正在尝试为三明治店创建一个菜单作为迷你项目。我已经弄清楚了我需要的大部分内容,但我想为每个菜单项分配一个价格整数,然后将每个数字相加得出最终价格。

 import pyinputplus as pyip


bread = ['White', 'Wheat', 'Sourdough'] #The Menu Options
protein = ['Chicken', 'Turkey', 'Ham', 'Tofu']
cheese = ['Cheddar', 'Swiss', 'Mozzarella']
sauce = ['Mayo', 'Mustard', 'Lettuce', 'Tomato']

OP1 = pyip.inputMenu(bread, prompt = 'What kind of bread? \n') #Asking for Bread
OP2 = pyip.inputMenu(protein, prompt = 'What kind of Protein? \n') #Asking for Protien
def OP3(): #Asking for Cheese and what kind they would like
    cyn = pyip.inputYesNo('Want any cheese? ')
    if cyn == 'yes':
        pyip.inputMenu(cheese, prompt = 'What kind of Cheese? \n')
def OP4(): #Asking for Sauce and what kind they would like
    syn = pyip.inputYesNo('Want any Sauce? ')
    if syn == 'yes':
        pyip.inputMenu(sauce, prompt = 'What kind of Sauce? \n')

OP3() #Calling the Cheese and Sauce Functions
OP4()

OP5 = pyip.inputNum(prompt='How Many Sandwiches would you Like? ') #Asking how many sandwiches they would like.

d = {'White': 3, 'Wheat': 1, 'Sourdough': 4, 'Chicken': 3, 'Turkey': 5, 'Ham': 3, 'Tofu': 9, 'Cheddar': 3, 'Swiss': 2, 'Mozzarella': 4, 'Mayo': 3, 'Mustard': 3, 'Lettuce': 3, 'Tomato': 90} #Dictionary I thought would convert the strings into numbers

print((OP1 + OP2 + OP3 + OP4) * OP5) #Final Price Equation

我尝试制作一个字典,以便将字符串转换为我稍后可以相加的整数,但它给了我一个类型错误“只能将 str(而不是”函数“)连接到 str

有谁知道根据用户输入将整数分配给字符串的好方法,以便以后可以添加它?

python 字符串 字典 整数

评论

1赞 Barmar 11/16/2023
您需要在字典中查找值。d[OP1] + d[OP2] ...
1赞 Barmar 11/16/2023
OP3并且是函数,而不是变量,您必须用 调用它们。然后需要归还一些东西。OP4()
0赞 twalk126 11/16/2023
我能够从打印 OP1 + OP2 开始,但是当我尝试使用 print(d[OP1] + d[OP2] * d[OP5]) 将这些变量的总和乘以 OP5 时,它只会向我抛出一个键错误,我为 OP5 提示输入的整数
0赞 Barmar 11/16/2023
OP5已经是一个数字,你不需要在字典中查找它。
0赞 twalk126 11/16/2023
我终于能够让它工作了。感谢您对字典的帮助。至于函数,我只是删除了它们并为 pyip.inputYesNo 创建了新变量,并在 if 语句中嵌套了 OP3 和 OP4,以检测用户是否输入 yes 或 no

答:

0赞 M Nastri 11/22/2023 #1

这部分与代码的其余部分不一致,并且不会将 OP3 或 OP4 设置为任何价格/成分

def OP3(): #Asking for Cheese and what kind they would like
    cyn = pyip.inputYesNo('Want any cheese? ')
    if cyn == 'yes':
        pyip.inputMenu(cheese, prompt = 'What kind of Cheese? \n')
def OP4(): #Asking for Sauce and what kind they would like
    syn = pyip.inputYesNo('Want any Sauce? ')
    if syn == 'yes':
        pyip.inputMenu(sauce, prompt = 'What kind of Sauce? \n')

OP3() #Calling the Cheese and Sauce Functions
OP4()

它应该是

cyn = pyip.inputYesNo('Want any cheese? ')
if cyn == 'yes':
    OP3 = pyip.inputMenu(cheese, prompt = 'What kind of Cheese? \n')

syn = pyip.inputYesNo('Want any Sauce? ')
if syn == 'yes':
    OP4 = pyip.inputMenu(sauce, prompt = 'What kind of Sauce? \n')

最后,您需要 dict 中的价格,因此您需要使用从 Inputs 中获取的键访问 dict 值,然后打印出来。

price_option_1 = d[OP1]
price_option_2 = d[OP2]
if cyn == 'yes': 
    price_option_3 = d[OP3] 
else: 
    price_option_3 = 0
if syn == 'yes': 
    price_option_4 = d[OP4] 
else: 
    price_option_4 = 0
quantity = OP5
total_price = (price_option_1 + price_option_2 + price_option_3 + price_option_4) * quantity

print(total_price)

说清楚,我啰嗦了,但您可以在变量命名中使用 或任何内容。p_OP1p_OP2