Python 3.4-将变量放入输入
我试图根据输入的两个答案计算总金额 榜样Python 3.4-将变量放入输入,python,python-3.x,Python,Python 3.x,我试图根据输入的两个答案计算总金额 榜样 type1_value=100 type2_value=200 model1_value=2 model2_value=3 typevalue=str(input('please enter the type: ')) modelvalue=str(input('please enter the model: ')) total=(typevalue*modelvalue) 如果客户第一个问题输入type1,第二个问题输入model2,我怎么
type1_value=100
type2_value=200
model1_value=2
model2_value=3
typevalue=str(input('please enter the type: '))
modelvalue=str(input('please enter the model: '))
total=(typevalue*modelvalue)
如果客户第一个问题输入type1,第二个问题输入model2,我怎么能
总共300人
为此感谢您,您需要的不是使用变量,而是一组值:
typevalues = {
"type1": 100,
"type2": 200
}
modelvalues = {
"model1": 2,
"model2": 3
}
type_ = str(input('please enter the type: '))
model = str(input('please enter the model: '))
total = typevalues[type_] * modelvalues[model]
使用字典:
types = {"type1_value": 100, "type2_value": 200}
type=str(input('please enter the type: '))
typevalue=types[type]
模型也一样
阅读有关词典的更多信息到目前为止,最简单的方法是使用词典:
type = {}
type['type1'] = 100
type['type2'] = 200
typevalue = str(input('please enter the type:'))
val = type[typevalue]
首先,您不需要将输入转换为字符串,因为它已经是字符串了。 要执行所需操作,必须检查输入并指定类型或模型值
type = input("Enter type: ")
model = input("Enter model: ")
if type == "type1":
type_value = 100
elif type == "type2":
type_value = 200
if model == "model1":
model_value = 2
elif model == "model2":
model_value = 3
total = type_value*model_value
你可以添加它们吗?这是不可能的。我很想回答,但这看起来像是家庭作业。没有任何规则禁止这样做吗?我想你在寻找这个: