python

Number Guessing Game Python 3.8


A random number between 1 and 6, this represents a roll of a dice. The random number becomes the number of allowed guesses from the user. I cannot get the dice number to be same as amount of guesses allowed.

This is what I have so far:

import random

number = random.randint(1, 100)
player_name = input("Hello, What's your name?")
number_of_guesses = 0
print('okay! '+ player_name+ ' I am guessing a number between 1 and 100:')
min_value = 1
max_value = 6
print(random.randint(min_value, max_value))
while number_of_guesses < 5:
   guess = int(input())
   number_of_guesses += 1
   if guess < number:
        print("Your guess is too low")
   if guess > number:
        print("Your guess is too high")
   if guess == number:
        break
   if guess == number:
        print("You guessed the number in " + str(number_of_guesses) + " tries!")
   else:
        print("You did not guess the number, the number was " + str(number))

Solution

  • OK, this should fix your issues:

    import random
    
    number = random.randint(1, 100)
    player_name = input("Hello, What's your name?")
    number_of_guesses = 0
    print('okay! '+ player_name+ ' I am guessing a number between 1 and 100:')
    
    max_guesses = random.randint(1, 6)
    print(f"You have {max_guesses} tries. ")
    
    won = False
    
    while number_of_guesses < max_guesses:
       guess = int(input())
       number_of_guesses += 1
       if guess < number:
            print("Your guess is too low")
       if guess > number:
            print("Your guess is too high")
       if guess == number:
            won = True
            break
    
    
    if won:
        print("You guessed the number in " + str(number_of_guesses) + " tries!")
    else:
        print("You did not guess the number, the number was " + str(number))
    

    However, this seems like some starting-out project, so it is important to understand what every code line is doing. In case you are not sure about anything, ask away :)