Python if statement problems

python if statement multiple conditions
python if statement string
if else python
if else python 3
python multiple if statements on one line
python if-else one line
if else questions in python
python statements

I am trying to make something work with if statements, but instead of choosing the correct one, it chooses all of them. So, the code below:

money=100

if money==100:
    print('You buy a pair of headphones')
    money-=100
if money<50:
    print('You buy a movie')
        money-=25
if money<10:
    print('You buy a burger')
    money-=5

Returns this:

You buy a pair of headphones
You buy a movie
You buy a burger

Please Help!

Use elif

money=100

if money==100:
    print('You buy a pair of headphones')
    money-=100
elif money<50:
    print('You buy a movie')
    money-=25
elif money<10:
    print('You buy a burger')
    money-=5

each possibility is now part of the same block, so only one decision will be made rather then the sequential run through you have with plain ifs

Python if, ifelse, ifelifelse and Nested if Statement, www.bowdoin.edu › ~ltoma › teaching › spring05 › Lectures › allif Python conditional statements and loops [44 exercises with solution] [An editor is available at the bottom of the page to write and execute the scripts.1. Write a Python program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and 2700 (both included).

When you have more than one condition and you want to execute only one if its match your criteria then use

"if elif keywords"

In your case, code will be

money = 100
if money == 100:
    print('You buy a pair of headphones')
    money -= 100
elif money <= 50 and money >=25:
    print('You buy a movie')
    money -= 25
elif money <= 10 and money >=5:
    print('You buy a burger')
    money -= 5

[PDF] [PDF] The if Statement and Practice Problems, where if is a reserved word, boolean-expression is an expression that evaluates to true or false, and statement is a C++ statement or a group of statements  Syntax of the if statement in Python: if test expression: statement(s) As depicted by the flowchart above, the Python program first evaluates the test expression. It is basically the condition in the if statement in Python. If the condition is met or if the condition is true, then only the statement(s) in the body of the if statement is(are) executed.

Or do something like this:

prices = {'100':'headphones', '50':'movie', '10':'burger', 5}
money = 100
for price in prices: 
    if money >= price: 
        print 'You buy a ', prices[price]
        money -= price
        break

It's not the same to the letter, but you can see that it's a lot more compact. It's easier to add a new case. And the logic makes sense - I should be able to buy anything that I have the money to pay for.

I'd encapsulate it in a method, too:

prices = {'100':'headphones', '50':'movie', '10':'burger', 5}
def buy(money):
    message = 'You could not afford anything'
    for price in prices: 
        if money >= price: 
            message = 'You buy a ' + prices[price]
            money -= price
            break
    return message

3.1. If Statements, The general Python syntax for a simple if statement is able to translate English descriptions of problems that would naturally include if or if - else statements. Python if Else Statement. A Python if else statement takes action irrespective of what the value of the expression is. If the result is True, then the code block following the expression would run. Otherwise, the code indented under the else clause would execute. Given below is the syntax of Python if Else statement.

This happens because all the conditions are true,so you get all the outputs. Instead

money = 100
if money == "100":
   print("You buy a pair of headphones.")
   money = 100
elif money <="50" and => "25":
    print("You buy a movie.")
    money = 25

elif money <= "25" and => "10":
     print("You buy a burger.")
     money = 5

Practice questions of Python - Decide if/else, A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net  In Python, If Statement is used for decision making. It will run the body of code only when IF statement is true. When you want to justify one condition while the other condition is not true, then you use "if statement".

Python If Else, If Elif Else Statements Explained with Examples, Let's now briefly see what this tutorial is going to cover. Python Conditions/​Decision-Making Statements. Table of Content. if Statement. Syntax; Flowchart; Example. In Python, the body of the if statement is indicated by the indentation. Body starts with an indentation and the first unindented line marks the end. Body starts with an indentation and the first unindented line marks the end.

If Else Statements, In Python, if-statements can include else clauses. An else clause is a section of code that runs if the if-statement is False . If the if-statement is True , the code  An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at most only one else statement following if.

Conditional Statements in Python – Real Python, Introduction to the if Statement; Grouping Statements: Indentation and Blocks. Python: It's All In a Python program, the if statement is how you perform this sort of decision-making. It allows for The Python pass statement solves this problem​. The if Statement and Practice Problems The Simple if Statement Use To specify the conditions under which a statement or group of statements should be executed.

Comments
  • I'm trying to understand...your answer is of course good, but why wouldn't the code from OP work in the case money is 100 ?
  • @Bartdude because immediately after it is decremented by 100 and then the next (separate) if statement runs