Why is this python loop not working as I expect?

Question:

Why does it not generate two random numbers untill theyre are 5 and 3?

import random

rolla = int(0)
rollb = int(0)


while rolla != 5 and rollb != 3:
  rolla = random.randint(1, 10)
  rollb = random.randint(1, 11)
  print(rolla, ' - ', rollb)



Asked By: Dom Wujek

||

Answers:

import random

rolla = 0
rollb = 0

while not(rolla == 5 and rollb == 3):
  rolla = random.randint(1, 10)
  rollb = random.randint(1, 11)
  print(rolla, ' - ', rollb)



you would have to use a not statement to encompass the entire phrase, otherwise it will stop if one of the conditions is false.

Answered By: Anton Chernyshov

Change to:

while not (rolla == 5 and rollb == 3):
Answered By: Omri
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.