While loop not executing passed the user input

Question:

#imports a library to call on random integers
import random

user_choice = ''
random_num1 = 0
random_num2 = 0
random_num3 = 0


#keeps the loop going until ended by the user
while user_choice != "n":

user_choice = input(f'Welcome to the slot machine, would you like to play (y)?: ')
random_num1 = random_num1 + random.randint(0, 10)
random_num2 = random_num2 + random.randint(0, 10)
random_num3 = random_num3 + random.randint(0, 10)



#prints the random integers side by side
print(random_num1, random_num2, random_num3)

if random_num1 == random_num2 and random_num2 == random_num3:
  print('Jackpot!!')
elif random_num1 == random_num2:
  print('Matched 2!!')
elif random_num1 == random_num3:
  print('Matched 2!!')
elif random_num2 == random_num3:
print('Matched 2!!')
else:
print('Sorry you lost!')

user_choice = input(f'Play again (y)?: ')

I’m sure there is more than one issue going on here, however it won’t go passed asking Welcome to the slot machine, would you like to play (y)?: for me to troubleshoot anything further, I’ve tried rearanging the different lines and formatting the random integers differently (at one point it was just infinitely printing 3 random integers) and I just can’t find a similar problem. I’m pretty new to python and any help is appreciated.

Asked By: MU7S

||

Answers:

You need to indent properly.

Code: [with proper indentation]

#imports a library to call on random integers
import random

user_choice = ''
random_num1 = 0
random_num2 = 0
random_num3 = 0


#keeps the loop going until ended by the user
while user_choice != "n":

    user_choice = input(f'Welcome to the slot machine, would you like to play (y)?: ')
    random_num1 = random_num1 + random.randint(0, 10)
    random_num2 = random_num2 + random.randint(0, 10)
    random_num3 = random_num3 + random.randint(0, 10)
    
    
    
    #prints the random integers side by side
    print(random_num1, random_num2, random_num3)
    
    if random_num1 == random_num2 and random_num2 == random_num3:
      print('Jackpot!!')
    elif random_num1 == random_num2:
      print('Matched 2!!')
    elif random_num1 == random_num3:
      print('Matched 2!!')
    elif random_num2 == random_num3:
      print('Matched 2!!')
    else:
      print('Sorry you lost!')
    
    user_choice = input(f'Play again (y)?: ')

Output:

Welcome to the slot machine, would you like to play (y)?: y
7 2 3
Sorry you lost!
Play again (y)?: y
Welcome to the slot machine, would you like to play (y)?: y
13 9 6
Sorry you lost!
Play again (y)?: y
Welcome to the slot machine, would you like to play (y)?: y
21 15 15
Matched 2!!
Play again (y)?: y
Welcome to the slot machine, would you like to play (y)?: y
24 20 17
Sorry you lost!
Play again (y)?: n
#while loop exit
Answered By: Yash Mehta
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.