ChalzZy
ChalzZy

Reputation: 25

My code won't select a new random number

I'm creating a mini project where a user has to guess the randomly selected number between 1 and 10.

My code works up to the point where the user guesses the correct number. It'll loop back up to "restart = 1" but won't generate a new number?

Here's a screenshot of what I mean - https://gyazo.com/1b6c9afc17997e7a0ee059a8b0eeb89e As you see in that circumstance, the number wouldn't change from 6?

It'd be awesome to have any help possible!

# Useful module for selecting random numbers
import random

# Return to this point when user guesses the correct number
restart = 1

while (restart < 10):
    # Variable that chooses a random number
    Random_Number = (random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))

    # Loop back to this point
    loop = 1

    # Checks if users number is correct or not
    while (loop < 10):

        personStr = input("Guess the random number from 1 - 10?: ")
        person = int(personStr)

        if person == Random_Number:
            print ("You are correct!, the number is", Random_Number)
            print ("Try to guess the new number!")
            restart = restart + 1

        elif Random_Number < person:
            print ("Your number is smaller than your selected number, try again!")
            loop = loop + 1

        else:
            print ("Your number is larger than your selected number, try again!")
            loop = loop + 1

Upvotes: 0

Views: 163

Answers (4)

A T
A T

Reputation: 13826

Sounds like you are seeking random.shuffle then use loop as your index, like so:

# Useful module for selecting random numbers
import random

# Return to this point when user guesses the correct number
restart = 0

rand_nums = random.shuffle(range(1, 11)).
# ^Assuming you only want one of each in range (1,11)

while restart < 10:
    restart += 1   
    # Loop back to this point
    loop = 1

    # Checks if users number is correct or not
    while loop < 10:
        loop += 1

        personStr = input("Guess the random number from 1 - 10?: ")
        person = int(personStr)

        if person == rand_nums[restart]:
            print ("You are correct!, the number is", rand_nums[restart])
            print ("Try to guess the new number!")
            break
        elif rand_nums[restart] < person:
            print ("Your number is smaller than your selected number, try again!")
        else:
            print ("Your number is larger than your selected number, try again!")

EDIT: And as others have said, you may be wanting to break out of your conditionals. The code above now includes that also.

Upvotes: -1

Ashutosh
Ashutosh

Reputation: 1050

Use Random_Number = random.randint(1, 10) for generating random integers from 1 to 10 and use break; after your conditional statements.

Upvotes: 0

awmleer
awmleer

Reputation: 1800

Add a break to exit the loop.

# Useful module for selecting random numbers
import random

# Return to this point when user guesses the correct number
restart = 1

while (restart < 10):
    # Variable that chooses a random number
    Random_Number = (random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))

    # Loop back to this point
    loop = 1

    # Checks if users number is correct or not
    while (loop < 10):

        personStr = input("Guess the random number from 1 - 10?: ")
        person = int(personStr)

        if person == Random_Number:
            print ("You are correct!, the number is", Random_Number)
            print ("Try to guess the new number!")
            restart = restart + 1
            break # add a break here

        elif Random_Number < person:
            print ("Your number is smaller than your selected number, try again!")
            loop = loop + 1

        else:
            print ("Your number is larger than your selected number, try again!")
            loop = loop + 1

Upvotes: 1

SatanDmytro
SatanDmytro

Reputation: 537

You have to add break for second loop:

if person == Random_Number:
    print ("You are correct!, the number is", Random_Number)
    print ("Try to guess the new number!")
    restart = restart + 1
    break

Upvotes: 1

Related Questions