-3
import random

a = 1

b = 2

c = 3

d = 4
# I would like the random_number to be 5

random_number = random.randint(1,5)

def function():
    global random_number
    while True:
        random_number = random.randint(1,5)
        if random_number !=  a or b or c or d:
            break
        print (random_number)


function()

down below is the link for the code

https://pastebin.com/bhmMkF81

So I am trying to make a program that will be able to play tic tac toe. But I have problem with generation of numbers that arent assigned to the variable.This code that I posted keeps giving me random numbers.

Kilianix
  • 1
  • 1
  • 1
    Please provide more clarification on your problem. This is unclear to me. – scharette Jul 13 '18 at 13:57
  • Welcome to StackOverflow, However, your question makes no sense to me. Please say exactly what your desired output is, your actual output, and why that actual output is wrong. Please read and follow [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Rory Daulton Jul 13 '18 at 13:58
  • 2
    `random_number != a or b or c or d` does not mean what you think it does. It is always true, because `b or c` is true, since all non negative numbers are. – Graipher Jul 13 '18 at 14:02
  • I think his request it's what is on the comment – Andre Motta Jul 13 '18 at 14:02
  • 1
    Change `if random_number != a or b or c or d:` into `if random_number not in (a, b, c, d)` and see for example [this question](https://stackoverflow.com/questions/24159301/always-true-when-testing-if-string-various-ored-alternatives) for more details. – Thierry Lathuille Jul 13 '18 at 14:08
  • just make the function return `5`. – frederick99 Jul 13 '18 at 14:14

2 Answers2

2

Assuming you want to loop inside function until random_number is 5, your conditions inside function are incorrect. The following will run the loop until random_number is 5 and print the result:

def function():
    global random_number
    print('here')
    while True:
        print('here2')
        random_number = random.randint(1,5)
        print(random_number)
        if random_number not in [a, b, c, d]:
            break
    print (random_number) # will always print 5

Note the condition random_number not in [a, b, c, d] checks that random_number is not equal to a, b, c, or d.

hoffee
  • 470
  • 3
  • 16
0

Your code checks wether the numbers are not 1 or 2 or 3 or 4.

If you want only 5 to show up you need to change it to and

The logic here is such that

if you get any number it will be not one of those 4 numbers below

~1 or ~2 or ~3 or ~4

It must be at the same time different from all of them therefore

if random_number !=  a and b and c and d:

I also will believe that this is all inside the while(True) but the print and it was just an indent error when copying the code here. So the code should be

import random

a = 1
b = 2
c = 3
d = 4
# I would like the random_number to be 5

random_number = random.randint(1,5)

def function():
    global random_number
    while True:
        random_number = random.randint(1,5)
        if (random_number != a) and (random_number != b) and (random_number != c) and (random_number != d):
            break
    print (random_number)


function()
Andre Motta
  • 759
  • 3
  • 16
  • 1
    `random_number != a and b and c and d` is not the same as `(random_number != a) and (random_number != b) and (random_number != c) and (random_number != d)` – hoffee Jul 13 '18 at 14:04
  • Of course just checking if the number is == 5 is much better. But i gave this example so you understand the logic of and and or – Andre Motta Jul 13 '18 at 14:04
  • `random_number not in [a, b, c, d]` is better I think. – hoffee Jul 13 '18 at 14:06
  • @hoffee yes but i wanted to elucidate the difference between or statements and and statements. random_number == 5 is the best solution of all i suppose. My sintax mistake stands corrected nonetheless – Andre Motta Jul 13 '18 at 14:07