Reputation: 35
import random
def guess_number_game():
number = random.randint(1, 101)
points = 0
print('You already have ' + str(points) + ' point(s)')
playing = True
while playing:
guess = int(input('Guess the number between 1 and 100: '))
if guess > number:
print('lower')
elif guess < number:
print('Higher')
else:
print('You got it, Good job!!')
playing = False
points += 1
play_again = True
while play_again:
again = input('Do you want to play again type yes/no: ')
if again == 'yes':
playing = True
play_again = False
elif again == 'no':
play_again = False
else:
print('please type yes or no')
print('Now you have ' + str(points) + ' point(s)')
guess_number_game()
i just started to learn python and i made this simple number guessing game, but if you try to play again you get the same number.
e.g. the number is 78 and you guessed it but you want to play again so you say you want to play again the number is still 78.
so how do i make it so that the number changes everytime someone plays the game
Upvotes: 0
Views: 1721
Reputation: 161
You define your random number outside of the game, set a new random number when someone decides to play again!
import random
def guess_number_game():
number = randbelow(1, 101)
points = 0
print('You already have ' + str(points) + ' point(s)')
playing = True
while playing:
print(number)
guess = int(input('Guess the number between 1 and 100: '))
if guess > number:
print('lower')
elif guess < number:
print('Higher')
else:
print('You got it, Good job!!')
playing = False
points += 1
play_again = True
while play_again:
again = input('Do you want to play again type yes/no: ')
if again == 'yes':
playing = True
play_again = False
number = randbelow(1, 101)
elif again == 'no':
play_again = False
else:
print('please type yes or no')
print('Now you have ' + str(points) + ' point(s)')
guess_number_game()
Upvotes: 0
Reputation: 382
import random
def guess_number_game():
points = 0
print('You already have ' + str(points) + ' point(s)')
playing = True
play_again=False
number = random.randint(1, 101)
#print(number)
while playing:
guess = int(input('Guess the number between 1 and 100: '))
if guess > number:
print('lower')
elif guess < number:
print('Higher')
else:
print('You got it, Good job!!')
number = random.randint(1, 101)
points += 1
again = input('Do you want to play again type yes/no: ')
if again == 'yes':
playing = True
elif again == 'no':
playing = False
print('Now you have ' + str(points) + ' point(s)')
guess_number_game()
Upvotes: 0
Reputation: 63
You need to set the number to the random generated number in the loop.
Example:
import random
def guess_number_game():
playing = True
while playing:
number = random.randint(1, 101)
points = 0
print('You already have ' + str(points) + ' point(s)')
playing = True
print(number)
guess = int(input('Guess the number between 1 and 100: '))
if guess > number:
print('lower')
elif guess < number:
print('Higher')
else:
print('You got it, Good job!!')
playing = False
points += 1
play_again = True
while play_again:
again = input('Do you want to play again type yes/no: ')
if again == 'yes':
playing = True
play_again = False
elif again == 'no':
play_again = False
else:
print('please type yes or no')
print('Now you have ' + str(points) + ' point(s)')
guess_number_game()
Upvotes: 1