简体   繁体   中英

I need help on a python guessing game

I need help changing the range and showing the user what the range is so they know if they are closer or not. I have given the description I have been given. On what I need to do. I have given the code that I have come up wit so far. Let me know if you need anything else from me.

Step 6 – Guiding the user with the range of values to select between

Add functionality so that when displaying the guess prompt it will display the current range to guess between based on the user's guesses accounting for values that are too high and too low. It will start out by stating What is your guess between 1 and 100, inclusive?, but as the user guesses the range will become smaller and smaller based on the value being higher or lower than what the user guessed, eg, What is your guess between 15 and 32, inclusive? The example output below should help clarify.

EXAMPLE

---------------- What is your guess between 1 and 44 inclusive? 2 Your guess was too low. Guess again.

import random
import sys

def main():
    print("Assignment 6 BY enter name.")
    welcome()
    play()

#Part 1
def welcome():
    print("Welcome to the guessing game. I have selected a number between 1 and 100 inclusive. ")
    print("Your goal is to guess it in as few guesses as possible. Let’s get started.")
    print("\n")




def play():
    ''' Plays a guessing game'''

    number = int(random.randrange(1,10))
   
    guess = int(input("What is your guess between 1 and 10 inclusive ?: "))
    number_of_guess = 0

     
   
    while guess != number :
        (number)

        #Quit
        if guess == -999:
            print("Thanks for Playing")
            sys.exit(0)

        
        #Guessing
        if guess < number:

            if guess < number:
                guess = int(input("Your guess was too low. Guess Again: "))
                number_of_guess += 1
                

            elif guess not in range(1,11):
                print("Invalid guess – out of range. Guess doesn’t count. : ")
                guess = int(input("Guess Again: "))

            else:
                guess = input("Soemthing went wrong guess again: ")
            
            
        if guess > number:

            if guess > number:
                guess = int(input("Your guess was too high. Guess Again: "))
                number_of_guess += 1

            elif guess not in range(1,11):
                print("Invalid guess – out of range. Guess doesn’t count. : ")
                guess = int(input("Guess Again: "))

            else:
                guess = input("Soemthing went wrong guess again: ")


                
        #Winner
        if guess == number :
            number_of_guess += 1
            print("Congratulations you won in " + str(number_of_guess) + " tries!")
            again()

    
  

                  
def again():
    ''' Prompts users if they want to go again'''

    redo = input("Do you want to play again (Y or N)?: ") 

 
    if redo.upper() == "Y":
        print("OK. Let’s play again.")
        play()
        
    elif redo.upper() == "N":
        print("OK. Have a good day.")
        sys.exit(0)
        
    
    else:
        print("I’m sorry, I do not understand that answer.")
        again()

            







main()

What you'll need is a place to hold the user's lowest and highest guess. Then you'd use those for the range checks, instead of the hardcoded 1 and 11 . With each guess, if it's a valid one, you then would compare it to the lowest and highest values, and if it's lower than the lowest then it sets the lowest value to the guess, and if it's higher than the highest it'll set the highest value to the guess. Lastly you'll need to update the input() string to display the lowest and highest guesses instead of a hardcoded '1' and '10' .

You need to simplify a lot your code. Like there is about 6 different places where you ask a new value, there sould be only one, also don't call method recursivly (call again() in again() ) and such call between again>play>again.

Use an outer while loop to run games, and inside it an inner while loop for the game, and most important keep track of lower_bound and upper_bound

import random
import sys

def main():
    print("Assignment 6 BY enter name.")
    welcome()
    redo = "Y"
    while redo.upper() == "Y":
        print("Let’s play")
        play()
        redo = input("Do you want to play again (Y or N)?: ")

def welcome():
    print("Welcome to the guessing game. I have selected a number between 1 and 100 inclusive. ")
    print("Your goal is to guess it in as few guesses as possible. Let’s get started.\n")

def play():
    lower_bound, upper_bound = 0, 100
    number = int(random.randrange(lower_bound, upper_bound))
    print(number)
    guess = -1
    number_of_guess = 0

    while guess != number:
        guess = int(input(f"What is your guess between {lower_bound} and {upper_bound - 1} inclusive ?: "))

        if guess == -999:
            print("Thanks for Playing")
            sys.exit(0)
        elif guess not in list(range(lower_bound, upper_bound)):
            print("You're outside the range")
            continue

        number_of_guess += 1
        if guess < number:
            print("Your guess was too low")
            lower_bound = guess
        elif guess > number:
            print("Your guess was too high")
            upper_bound = guess

    print("Congratulations you won in", number_of_guess, "tries!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM