How can I use dictionary as a conditional statement to change the value of the variable?
for example:
a = 20
dicta = {10:3, 20:2, 30:1}
#compare using first pair as the value of a, such that:
if a=10: assign a=3
if a=20: assign a=2
if a=30: assign a=1
Thankyou!
CodePudding user response:
If you can be sure that the value of a is actually a key in the dict:
a = dicta[a]
or to have an additional check:
if a in dicta:
a = dicta[a]
else:
print("error, value {} not in dictionary".format(a))
CodePudding user response:
Try this:
a = 20
for k, v in dicta.items():
if k == a:
a = v
break
