Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I am currently working on a number guessing game, in that a random digit between 1 and 6, denotes a roll of a dice. The random number will then become the number of allowed guesses from the user. I am not able to get the dice number to be the same as the number 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))

1 Answer

0 votes
by (108k points)

I have gone through your code and there were some issues in the code. I have fixed the issue, kindly check in the below program:

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))

Still, this looks like some starting-out project, so it is very necessary to know what every code line is executing. 

Want to be a Python expert? Join this Python Training course by Intellipaat to learn more.

Browse Categories

...