Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While loop not executing passed the user input

#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.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading