0

I have problems with the or operator. I'm just starting out with Python. I should print in the textbox either A or B. Can't. Problem is, only A. B does not print. How can I do? Thank you

A = f"{name} {''.join(word2)} {inhabitants} inhabitants on an area of {surface}"
B = f"Che sale a"
        
text.insert(tk.END, A or B)
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34

2 Answers2

2

You seem to want to randomly select to display A or B. You can do this with the pre-installed random module. random.choice takes in a list and randomly returns one element from that list, which is the desired behavior.

import random

A = f"{name} {''.join(word2)} {inhabitants} inhabitants on an area of {surface}"
B = f"Che sale a"
        
text.insert(tk.END, random.choice([A, B]))

If you would like to use a more secure version of random.choice, you can do so with the pre-installed secrets module

import secrets

A = f"{name} {''.join(word2)} {inhabitants} inhabitants on an area of {surface}"
B = f"Che sale a"
        
text.insert(tk.END, secrets.choice([A, B]))
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
1

You need to create an if statement to decide in what condition A or B should be printed. Or is actually used with booleans. If either of the booleans are true, or will return true.

Shonsave
  • 78
  • 8
  • 1
    Not exactly. `or`, the python version of `||` actually returns the first truthy value. Look here for an example: https://wandbox.org/permlink/zqNl4LfeK8KeS4cf (source code for future readers: `print("" or "Hello, World"); print(5 or 6); print(0 or 6); print(False or None); print(None or "Hello")`) – Samathingamajig Jun 17 '21 at 05:05
  • 1
    Wow, I didn't know that it could also be used like that.. Thanks for the info! – Shonsave Jun 17 '21 at 06:18