I am trying to create a number guessing game in python 3.7 and am stuck on the difficulty levels which needs to be by how many guesses

here is the objectives for the code:

  1. You are to ask the user if they wish to play the game or continue playing the game after they have guessed the number

  2. You are to ask the user if they want easy, medium or HARD (Easy = unlimited guesses, Medium = 10 guesses, HARD = 5 guesses)

  3. Once the game begins play you are to tell the user that you have picked a number and they are to guess what that number is

  4. your application is to accept the players guess and if their guess is larger than the number you had picked then tell them that, if their guess is smaller then tell them that as well.

  5. The game continues to play until the player picks the number you had originally picked or they have ran out of guesses.

Here is my code so far:

# This is a guessing the number game.

import random
# this statement will allow the options for playing and quiting the game.
play = True

while play:

    difficulty = 0
    
    guessesTaken = 0

    print('Hello! What is your name?')

    myName = input()

    easy = random.randint (1,100)
    easy = random.randint (1,100)
    easy = random.randint (1,100)

      
#prompts the user to select a difficulty to play on
    def select_level():    
        while True:
            level = str(input("Would you like to play on easy, medium, or hard? \n"
                          "Type 'e' for easy, 'm' for medium, or 'h' for hard!\n"))
            if level != "e" and level != "m" and level != "h":
                print("Invalid input!\n")
            if level == "e" or level == "m" or level == "h":
                break
        return level

#function that prompts the user to guess depending on chosen difficulty
    def guess_number(level):        
        if level == "e":
            (easy == 500)
        if level == "m":
            (medium == 10)
        if level == "h":
            (hard == 5)
        return guesses

#selects number of guesses depending on difficulty selected
    def get_guesses(level):                  
            if difficulty == easy:
                print ("Okay, " + myName + ". You have unlimited guesses")
            if difficulty == medium:
                print ("Okay, " + myName + ". You have 10 guesses.")
            if diffuculty == hard:
                print ("Okay, " + myName + ". You have 5 guesses.")
            elif level != "e" and level != "m" and level != "h":
                print ("Invalid input!")
                get_guesses()
            return guesses
        
    level = select_level()
    guesses = get_guesses(level)

    print('Well, ' + myName + ', I am thinking of a number can you guess it?')

     
    while guessesTaken < level:
         print('Take a guess.') 
         guess = input()
         guess = int(guess)

         guessesTaken = guessesTaken + 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:
        guessesTaken = str(guessesTaken)
        print('Good job, ' + myName + '! You guessed the number in ' + guessesTaken + ' guesses!')

    if guess != number:
        number = str(number)
        print('Nope. The number I was thinking of was ' + number)

    count=1
    again=str(input("Do you want to play again, type yes or no "))
    if again == "no":
      play = False

With what exactly you are having problem? You are already keeping the number of guesses taken, now you need to correct your way of checking whether it’s under the assigned tries limit and if not then end the current game.

Okay this may sound stupid but I’ve only been using python since the 13th but I think that is where I am stuck is how do I assign the tries limit?

I really have no idea. I got help from someone who I think knew less than me.

Right now in while guessesTaken < level: you are comparing number of taken guesses to the letter equivalent of the difficulty level (e, m or h - which gets assigned in the level = select_level()). You want to assign to level the allowed number of tries before entering while loop, or use a different variable for that.

@ajj71310 Were you able to figure out your code? I looked at it, but decided to leave it to you to work on. (Although programming can be frusterating, it’s good to work through the hard). If you are still having trouble please upload a photo of the error you’re getting with the updated code. I can look at it.

I took bits of your code and created my own. It works and ensures that the while loops perform until an answer is given.

Feel free to critique my code if you like and feel free to use it.


# This is a guessing the number game.
import random

# User's name
print('Hello! What is your name? ')
myName = input()
print("Hi " + str(myName) + "!" + " Let's play a guessing game.")


def random_game():

    play = True
    while play:

        easy = random.randint(1, 100)
        medium = random.randint(1, 100)
        hard = random.randint(1, 100)
        # prompts the user to select a difficulty to play on
        print("Would you like to play on easy, medium, or hard?"
              "\nType 'e' for easy, 'm' for medium, or 'h' for hard!")

        level_selection = True
        while level_selection:
            level = input()
            if level == "e":
                print("\nAwesome! We'll begin with easy!")
                level_selection = not True
                break
            if level == "m":
                print("\nAwesome! We'll begin with medium!")
                level_selection = not True
                break
            if level == "h":
                print("\nAwesome! We'll begin with hard!")
                level_selection = not True
                break
            else:
                print("Invalid input!\nPlease enter either e, m, or h. ")


    # If the user selects e for Easy - 20 tries
        if level == 'e':
            print("Because you selected easy, you'll have 20\nguesses to guess a number between 1-100.")
            guesses_left = 20
            while guesses_left != 0:
                try1 = int(input("So, what's your guess? \n"))
                if try1 == easy:
                    print("That's it, you've guessed the right number! " + str(easy))
                    print("Congratulations " + str(myName) + "!")
                    break
                elif try1 < easy:
                    print("Nope, not quite! Guess higher!")
                elif try1 > easy:
                    print("Nope, you're a little high. Guess lower!")
                guesses_left -= 1
                print('You now have ' + str(guesses_left) + ' guesses left!')

                # Fun 5 tries left count down
                if guesses_left == 5:
                    print("Common', you can do this!\n\(^ω^\)")
                if guesses_left == 4:
                    print("That's ok, it's on Easy.\n¯\_(ツ)_/¯")
                if guesses_left == 3:
                    print("You're running out of guesses!\nᶘ ᵒᴥᵒᶅ")
                if guesses_left == 2:
                    print("Just 2 guesses left. Can the impossible happen?\n( ͡° ͜ʖ ͡°)")
                if guesses_left == 1:
                    print("By the old Gods and the New.\nYou've got 1 guess left.\n😨")

                # If the user runs out of guesses
                if guesses_left == 0:
                    print('Better luck next time!\n(╯° °)╯︵ ┻━┻')

    # If the user selects m for Medium - 10 tries
        if level == 'm':
            print("Because you selected medium, you'll have 10\nguesses to guess a number between 1-100.")
            guesses_left = 10
            while guesses_left != 0:
                try1 = int(input("So, what's your guess? \n"))
                if try1 == medium:
                    print("That's it, you've guessed the right number! " + str(medium))
                    print("Congratulations " + str(myName) + "!")
                    break
                elif try1 < medium:
                    print("Nope, not quite! Guess higher!")
                elif try1 > medium:
                    print("Nope, you're a little high. Guess lower!")
                guesses_left -= 1
                print('You now have ' + str(guesses_left) + ' guesses left!')

                # Fun 5 tries left count down
                if guesses_left == 5:
                    print("Common', you can do this!\n\(^ω^\)")
                if guesses_left == 4:
                    print("That's ok, it's on Medium.\n¯\_(ツ)_/¯")
                if guesses_left == 3:
                    print("You're running out of guesses!\nᶘ ᵒᴥᵒᶅ")
                if guesses_left == 2:
                    print("Just 2 guesses left. Can the impossible happen?\n( ͡° ͜ʖ ͡°)")
                if guesses_left == 1:
                    print("By the old Gods and the New.\nYou've got 1 guess left.\n😨")

                # If the user runs out of guesses
                if guesses_left == 0:
                    print('Better luck next time!\n(╯° °)╯︵ ┻━┻')

    # If the user selects h for Hard - 5 tries
        if level == 'h':
            print("Because you selected hard, you'll have 5\nguesses to guess a number between 1-100.")
            guesses_left = 5
            while guesses_left != 0:
                try1 = int(input("So, what's your guess? \n"))
                if try1 == hard:
                    print("That's it, you've guessed the right number! " + str(hard))
                    print("Congratulations " + str(myName) + "!")
                    break
                elif try1 < hard:
                    print("Nope, not quite! Guess higher!")
                elif try1 > hard:
                    print("Nope, you're a little high. Guess lower!")
                guesses_left -= 1
                print('You now have ' + str(guesses_left) + ' guesses left!')

                # Fun 4 tries left count down - 5 won't work for hard because you begin at 5
                if guesses_left == 4:
                    print("That's ok, it's on Hard.\n¯\_(ツ)_/¯")
                if guesses_left == 3:
                    print("Common', you can do this!\n\(^ω^\)")
                if guesses_left == 2:
                    print("Just 2 guesses left. Can the impossible happen?\n( ͡° ͜ʖ ͡°)")
                if guesses_left == 1:
                    print("By the old Gods and the New.\nYou've got 1 guess left.\n😨")

                # If the user runs out of guesses
                if guesses_left == 0:
                    print('Better luck next time!\n(╯° °)╯︵ ┻━┻')

        maybe_play = True
        while maybe_play:
            again = input("\nWould you care to play again?\nYes or No\n ")
            if again == 'No' or again == 'no':
                print('\nOk, thank you for playing.\nFeel free to come back any time!')
                maybe_play = not True
                play = not True
            elif again == 'yes' or again == 'Yes':
                print("\nCool, let's play again!👍\n")
                maybe_play = not True
                play = True
            else:
                print('Please enter either yes or no.')
                input()
                maybe_play = not True
                play = not True


random_game()

Yes I figured it out! I actually redid almost all of the code but I got it to do what I wanted! I like the fun 5 tries section of yours would have been cool to use in my code.

# This is a guessing the number game.

import random
# This statement will allow the options for playing and quiting the game.
play = True

while play:

    # Start questions for name and level of difficulty
    myName = input('Hello! What is your name? ')
    level = int(input('Pick a level of difficulty 1(easy), 2(medium), 3(hard): '))

    # Change number of allowed guesses with level
    if level == 1:
        top = 100
        tries = 10000
    elif level == 2:
        top = 100
        tries = 10
    elif level == 3:
        top = 100
        tries = 5
    else:
        top = 100
        tries = 10

    # Print statements for chosen level to tell how many guesses user gets                
    if level == 1:
        print ("Okay, " + myName + ". You have choosen easy. You have unlimited guesses")
    if level == 2:
        print ("Okay, " + myName + ". You have choosen medium. You have 10 guesses.")
    if level == 3:
        print ("Okay, " + myName + ". You have choosen hard. You have 5 guesses.")
    elif level != 1 and level != 2 and level != 3:
        print ("Invalid input!")

    # Picks the random integer and tracks number of guesses. Also tells in the number guessed is too high or low.
    number = random.randint(1, 100)
    print('Try to guess the number! Its between 1 and 100.')
    guessesTaken = 0
    while guessesTaken <= tries:
        guess = int(input('Take a guess: '))
        guessesTaken += 1
        if guess < number:
            print('Your guess is too low.')     
        if guess > number:
            print('Your guess is too high.')
        if guess == number:
            break

    # End statements depending on whether user guessed correct number or not.    
    if guess == number:
        guessesTaken = str(guessesTaken)
        print('Good job, ' + myName + '! You guessed the number in ' + guessesTaken + ' guesses!')

    if guess != number:
        number = str(number)
        print('Nope. The number was ' + number)

    # Play again loop that attaches to original statement that allows you to quit or play again.
    count=1
    again=str(input("Do you want to play again, type yes or no "))
    if again == "no":
        play = False
2 Likes

That’s awesome! I like your code. It’s clean and easy to read.