Python Forum
Problems with if / else statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with if / else statement
#1
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)
Reply
#2
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.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply
#3
Thank you!
Reply
#4
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
hi buran,

what is the '1' in line 4 represent ?
Reply
#6
It's the default value. See the docs for dict's get method here.
Reply
#7
thank you...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Having problems using 'or' in a 'if' statement? umut3806 2 2,094 Jul-21-2019, 11:33 PM
Last Post: umut3806
  problems with the If statement or is it the variables being used NickIgoe 2 2,134 Mar-22-2019, 06:34 AM
Last Post: NickIgoe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020