Below is my example code with what I am having trouble with. I can not figure out how to make it when you input t to make it print what 't' equals every time I enter 't' it prints 'h' and I stuck.
Please help I am a beginner to python.
money = int(input('amount of money'))
multiplier = str(input('enter t or h for thousands or hundreds:'))
t = money * 1000
h = money * 100
if multiplier == t:
print(t)
else:
print(h)
money = int(input('amount of money: '))
multiplier = (input('enter t or h for thousands or hundreds: '))
if multiplier == "t":
print(money*1000)
else:
print(money*100)
every input,python treat it as a string only so str() was not needed.
money = int(input('amount of money: '))
t = money * 1000
h = money * 100
multiplier =(input('enter t or h for thousands or hundreds: '))
if multiplier =="t":
print(t)
else:
print(h)
variable assignment was wrong as python execute code line by line and t and h you recieve from input are string so you have to use "" for using == operator.
isn't the logic reverse - i.e. if the amount is 5000, in thousands it will be 5 (i.e. divide, not multiply by 1000)?
Second in either case it's better to do one calculation - requested by user, not all calculations in advance
money = int(input('amount of money: '))
multiplier = input('enter t or h for thousands or hundreds: ') # ne need of extra brackets
if multiplier == "t":
print(money / 1000)
else:
print(money / 100)
even more pythonic would be
scales = {'t':1000, 'h':100} # you can expand this as you wish, without need to change rest of code
money = int(input('amount of money: '))
user_choice = input('enter t or h for thousands or hundreds: ') # ne need of extra brackets
divisor = scales.get(user_choice, 1)
print(money / divisor)
hi buran,
what is the '1' in line 4 represent ?
It's the default value. See the docs for
dict
's
get
method
here.