-7

I have a boolean called programRepeated and I need to check if it is true or false with an if / else statement. Is there a way to do this?

if programRepeated = True:
            print("Your balance is : $" , final_balance + cash_insert)
            print("")
        elif programRepeated = False:
            print("Your balance is : $" , final_balance + cash_insert - 5)
            print("")
  • 2
    = is assignment. Refer to a tutorial, which should also cover == (comparison). Also, can just use `if x:`, when x is truth’y-false’y. – user2864740 Sep 14 '20 at 00:08
  • 1
    A boolean does not need to be compared. That's kind of the point of a boolean. Just use `if programRepeated:` – Robby the Belgian Sep 14 '20 at 00:09
  • 2
    Can it ever be anything other than `True` or `False`, and if so, are those other values supposed to be treated as truthy, falsy or neither? I'll note this is *really* basic Python; I'd strongly suggest running through [a tutorial on the language as a whole](https://docs.python.org/3/tutorial/), because learning the basics piecemeal with Stack Overflow question just isn't practical. – ShadowRanger Sep 14 '20 at 00:11
  • Does this answer your question? [How do I use a Boolean in Python?](https://stackoverflow.com/questions/1748641/how-do-i-use-a-boolean-in-python) – The Grand J Sep 14 '20 at 00:13
  • Beware, objects are considered True by default unless they meet the criteria for False: https://stackoverflow.com/questions/63876672/how-do-i-check-if-a-boolean-is-true-or-false#comment124767447_63876691 – Chris Jan 04 '22 at 14:42

2 Answers2

1

A boolean value can go directly into a conditional (if) statement

if programRepeated:

If programRepeated is equal to true, then the code block will execute.

Some notes on your code though: = is an assignment operator. If you would like to check if something is equal, please use two equal signs ==. Your code will throw a syntax error if you try to use = in a conditional.

Also, your elif should be on the same indentation as the if statement, and you can use else instead in this case.

if programRepeated:
     print("Your balance is : $" , final_balance + cash_insert)
     print("")
else:
     print("Your balance is : $" , final_balance + cash_insert - 5)
     print("")
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Saddy
  • 1,515
  • 1
  • 9
  • 20
  • `=` wouldn't make it always true, this isn't C, and using `=` in a conditional isn't legal code; it would die with a `SyntaxError` during parsing. – ShadowRanger Sep 14 '20 at 00:13
  • Beware, "an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object". is_launch_nuke = "Don't launch!!!!" launch_nuke() if is_launch_nuke else print("Phew"). Results in nuke lanuched. – Chris Jan 04 '22 at 14:31
0

well, you don't need to explicitly add the = True, you can just do:

if programRepeated:
            print("Your balance is : $" , final_balance + cash_insert)
            print("")
        else:
            print("Your balance is : $" , final_balance + cash_insert - 5)
            print("")
Didier Prophete
  • 1,834
  • 1
  • 12
  • 11