Python Forum
Problems with if / else statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problems with if / else statement (/thread-26025.html)



Problems with if / else statement - droid206 - Apr-19-2020

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)


RE: Problems with if / else statement - Shahmadhur13 - Apr-19-2020

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.


RE: Problems with if / else statement - droid206 - Apr-19-2020

Thank you!


RE: Problems with if / else statement - buran - Apr-19-2020

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)



RE: Problems with if / else statement - astral_travel - Apr-19-2020

hi buran,

what is the '1' in line 4 represent ?


RE: Problems with if / else statement - ndc85430 - Apr-19-2020

It's the default value. See the docs for dict's get method here.


RE: Problems with if / else statement - astral_travel - Apr-19-2020

thank you...