Why this dont work?

print(“hello world”) or print(“hello”)

That give me the 2 answer, not 1

image

What are you expecting to happen when you use the or keyword?
What python is doing is a boolean or operation is that what you wanted?

The print() function does not return a boolean result. It returns None that is treated as False.

$ python3
Python 3.12.4 (main, Jun  7 2024, 00:00:00) [GCC 14.1.1 20240607 (Red Hat 14.1.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = print('Hello')
Hello
>>> print(x)
None
>>>

i expect to when i type an number, for exemple: 33, and that number i type is correct for continue the code, the code will print: (“correct”) , or print(“you are correct”) but that dont work, print one thing or print other thing. code:
number = int( input(“number:”) )

import random

number = random.randint(0,0)

if number < number:
print(“incorrect”)
elif number > number:
print(“incorrect”)
else:
print(“correct”) or print(“you are correct”)

when i write the “number” on terminal i want to he return me with “correct” or “you are correct” but he give me the 2 print, not only 1:
python l.py
number:0
correct
you are correct

Please demonstrate how you would like the output to look.

Indentation is important to python programs and for use to see the how you are coding we need to see that indentation. This forum makes that easy to do using the pre-formatted text feature. Its on the </> button, but you can also type the markup directly into your posts like this:

```
def myfunction():
     print('Hello')
```

Please edit you post so to add the back-tick markup so your code can be understood.

Your code:

 import random

 number = random.randint(0,0)

 if number < number:
     print("incorrect")
 elif number > number:
     print("incorrect")
 else:
     print("correct") or print("you are correct")

The messages in your output are:

 correct
 you are correct

Where do you think these 2 messages come from in the code above?

Why do you think that only one of these messages should be printed?
Not: because that’s what you wanted it to do, but: by what logical
mechanism does this line:

 print("correct") or print("you are correct")

print only one message. As you see when you run it, both messages are
printed.

What is your understanding of how Python’s or logical operator works?

1 Like