假设我有一个称为温度的变量。我希望它的价值根据情况而有所不同。通常我会做这样的事情:
temperature = None
country = input()
if country == 'USA' :
temperature = 10
elif country == 'Spain':
temperature = 20
elif country == 'Peru':
temperature = 30
#and so on
但是,如果我有很多价值观,那将永远。我觉得我需要使用集合,但我还没有开始使用它们(和字典)。但是,如果那是这样做的方式,我想知道。
你的意思是这样吗?
country = input()
cond = {'USA': 10, 'Spain': 20, 'Peru': 30 }
temp = cond.get( country, None )
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句