比如创建这样一个字典price={"product(1)":"100","product(2)":"58","product(3)":"20.8",}
当用户输入数字,赋给变量button,怎么用product(button)查询相应的键值
# -*- coding:utf-8 -*-
price = {"product(1)":"100", "product(2)":"58", "product(3)":"20.8",}
def product(num):
"get price"
get_price = "product(%d)" % num
print price[get_price]
button = raw_input("输入一个数字: ")
product(button)
product(1) # 100
price = {"product(1)":"100", "product(2)":"58", "product(3)":"20.8",}
def product(num):
"get price"
get_price = "product(%d)" % num
print price[get_price]
button = raw_input("输入一个数字: ")
product(button)
product(1) # 100
这是其中一个方式
温馨提示:答案为网友推荐,仅供参考