Home > database >  Python beginner slot machine
Python beginner slot machine

Time:10-17

I keep getting stuck in an infinite loop with this program. I know I need to assign separate values for each random number but I don't know how and my TAs aren't answering their emails. Here's my code:

import random
random_num = random.randint(0, 10) #generates random numbers between 0 and 10
user_input = input('Play again?(Y or N):')

while user_input != 'N':
    print('Python Slot Machine')
    print(random)

    if random_num != random_num and random_num != random_num:
        print('Uh oh! No match this time!')
        print(user_input)
    elif random_num == random_num and random_num != random_num:
        print('You matched two! So close!')
        print(user_input)
    elif random_num == random_num and random_num == random_num and random_num == random_num:
        print('Jackpot!! You matched all 3!')
        print(user_input)

CodePudding user response:

You are getting stuck in an infinite loops because your input function is outside of the loop, so the user never gets to decide whether they want to continue or not.

What you've done in your code above is generated a random number, and then made several references to that random number. Each time you place the variable randon_num into your function, it does not generate a new number. Instead, it keeps referencing back to when you defined random_num.

A simple way to do this would be to create a function that generates a random number so you don't have to copy the random.randint() code every time.

CodePudding user response:

if else statments dont work the way you think they do. If you have multiple loops, you can't check the value multiple times to address the loops. Also you checked random_num against random_num.

Let me show you another way to do this. Instead of giving the user a way to exit, have him play until the end. After 3 guesses it's game over anyways. For each time the user matched the random_number you give him a point. You also have to refresh the random number after each try, otherwise it will stay the same. And as you show the number after a loop, the user knows the number from there on.

After three loops you end the while loop and print one of the messages from the messages list using the earned points as index for the list.

import random

points = 0
counter = 0
messages = [
    'Uh oh! No match this time!',
    'You only matched one',
    'You matched two! So close!',
    'Jackpot!! You matched all 3!'
]
while counter < 3:
    random_num = random.randint(0, 10)  # generates random numbers between 0 and 10
    user_input = input('Please enter your guess: ')
    print('Python Slot Machine')
    print(f'random number: {random_num}')

    if random_num == int(user_input):
        points  = 1
    counter  = 1

print(messages[points])

CodePudding user response:

Your Logic don't make sense anyway. but, Correct structure is

import random
random_num = random.randint(0, 10)

while True:

    user_input = input('Play again?(Y or N):')

    if user_input == "Y":

        #your code
        #your code
        #Your code

    else:
        print("User want to quit")
        break
  • Related