Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python why wont this work? (using or in if statements)

richodude

New Coder
Python:
total = 3

def function():
    global total
    if total == 1 or 2:
        print(total)
        print("I'm a function that lets you know that your number is 1 or 2!")
    else:
        print("This is to let you know your number ISNT 1 or 2!")
        
def function_quotes():
    global total
    if total == "1" or "2":
        print(total)
        print("I'm a function that lets you know that your number is 1 or 2!")
    else:
        print("This is to let you know your number ISNT 1 or 2!")
            
function()
function_quotes()

obviously the expected result is for the code to skip the if lines entirely
 
Should be
If total == 1 or total == 2
This will test if 1 or 2 is true. If it's false then the else will execute.
On a side note, it is generally bad practice to use global.
Python:
total = 3 #This is already a global and can be accessed in the function

def func():
    print(total)
func()

or you could do something like

Python:
def func(arg):
    numbers = [1,2,5,9]
    if arg in numbers:
        print(f'{arg} is accepted.')
    else:
        print(f'{arg} is not accepted')

for i in range(10):
    func(i)

output
Code:
0 is not accepted
1 is accepted
2 is accepted
3 is not accepted
4 is not accepted
5 is accepted
6 is not accepted
7 is not accepted
8 is not accepted
9 is accepted
 
Last edited:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom