Python Forum
Problems with if / else statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with if / else statement
#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


Messages In This Thread
Problems with if / else statement - by droid206 - Apr-19-2020, 12:44 AM
RE: Problems with if / else statement - by droid206 - Apr-19-2020, 05:56 AM
RE: Problems with if / else statement - by buran - Apr-19-2020, 07:17 AM
RE: Problems with if / else statement - by ndc85430 - Apr-19-2020, 07:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Having problems using 'or' in a 'if' statement? umut3806 2 2,182 Jul-21-2019, 11:33 PM
Last Post: umut3806
  problems with the If statement or is it the variables being used NickIgoe 2 2,226 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