Python Forum
Higher or lower game - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Higher or lower game (/thread-39585.html)

Pages: 1 2


RE: Higher or lower game - jefsummers - Mar-15-2023

The following is 95% your code, with it just rearranged. There are a few things I would do differently, but this shows the organization that @deanhystad and I were referring to.
There is a top_level that handles a few definitions as well as the master loop (play once then ask if wants to play again)
There is a game_level that sets up the number of guesses, the target number, and sets up the loop for guesses
There is a handle_guess function that handles the guess

The functions are passed in what they need - avoided globals.
You had all the pieces, just not in an organized manner. I used your code, rearranged it and put it into more functions. Note that we are not supposed to write the program for you as it is homework. I hope I did not violate the rules as I did use your code and basic logic.
import random
#welcome banner
def welcome_banner():
    print("| ----------------------------------------- |")
    print("| |")
    print("| welcome to higher or lower |")
    print("| |")
    print("| ----------------------------------------- |")
#goodbye banner
def goodbye_banner():
    print("| ----------------------------------------- |")
    print("| |")
    print("| thanks for playing |")
    print("| |")
    print("| ----------------------------------------- |")

#Top Level Loop
def top_level():
    correct = ["well done you got the number correct",
    "good job you got it right"]
    wrong = ["good try but no succes",
    "tough luck you didn't guess my number"]
    while True:
        game_level(correct, wrong)
        again = input("do you want to play again? enter y to play again or n to quit")
        if again == ("n"):
            goodbye_banner()
            return

#Game Level
def game_level(correct, wrong):
    welcome_banner()
    #asking the user for their name and then welcoming them by name
    name = input("what is your name - ")
    print (f"Greetings {name} welcome to the guessing game")
    guess = 5
    pc_choice = random.randint(1,20)
    correct_comment = random.choice(correct)
    wrong_comment = random.choice(wrong)
    while guess > 0:
        got_it = handle_guess(pc_choice,name)
        if got_it == False:
            guess -=1
            print(f"you have {guess} guesses left")
        else:
            guess = 0
    if got_it == False:
        print(f"{wrong_comment}{pc_choice}")
        return
    else:
        print(f"{correct_comment}")
        return

#Turn level
def handle_guess(pc_choice, name):
    bad_value = True
    while bad_value:
        try:
            answer = int(input(f"{name} guess a number between 1 and 20: "))
            if answer < 1 or answer > 20:
                print("please only enter a number between 1 and 20")
            else:
                bad_value = False
        except ValueError:
            print("Please only enter an integer between 1 and 20")
    if answer > pc_choice:
        print("your guess is too high guess lower")
    elif answer < pc_choice:
        print("your guess is too low guess higher")
    elif answer == pc_choice:
        return True
    return False

top_level()



RE: Higher or lower game - SEWII - Mar-15-2023

thanks


RE: Higher or lower game - SEWII - Mar-15-2023

I have now added a for loop but my game doesn't end and show the goodbye banner when asked to play again or if you want to play again that doesn't work either


RE: Higher or lower game - jefsummers - Mar-15-2023

When you post code please use the Python tags in the toolbar. Otherwise you lose indentation.

What you posted does not seem to have changed/fixed problems pointed out. What I had posted works. With your code, try entering 21. What happens?


RE: Higher or lower game - deanhystad - Mar-15-2023

Please use python tags when you post your code. I saw you tried. Next time review before hitting the post button.

Since you decided to ignore any advice, what is the purpose of offering more? Break your code into functions as advised, and the error will be obvious.

You are using the wrong variable.
while True:
    again = input("play again? enter y to play again or n to quit")
    if again == ("n"):
        break
The outer game loop runs until "play" != "y", but here you use the variable "again".

Instead of this:
try:
    answer = int(input(f"{name} guess a number between 1 and 20: "))
    if answer>= 1 and answer<= 20:
        break
    else:
        print("please only enter a number between 1 and 20")
except ValueError:
    print("Please only enter an integer between 1 and 20")
Use the exception you are already catching.
for count in range(1, 6):
    while True:
        try:
            guess = int(input("Guess a number between 1 and 20: "))
            if guess < 1 or guess > 20:
                raise ValueError
            break
        except ValueError:
            print("Please only enter an integer between 1 and 20")



RE: Higher or lower game - SEWII - Mar-15-2023

(Mar-15-2023, 07:00 PM)deanhystad Wrote: Please use python tags when you post your code. I saw you tried. Next time review before hitting the post button.

Since you decided to ignore any advice, what is the purpose of offering more? Break your code into functions as advised, and the error will be obvious.

You are using the wrong variable.
while True:
    again = input("play again? enter y to play again or n to quit")
    if again == ("n"):
        break
The outer game loop runs until "play" != "y", but here you use the variable "again".

Sorry about that I didn't see your previous post I didn't mean to ignore it but thanks for your time and help


RE: Higher or lower game - SEWII - Mar-15-2023

(Mar-15-2023, 06:56 PM)jefsummers Wrote: When you post code please use the Python tags in the toolbar. Otherwise you lose indentation.

What you posted does not seem to have changed/fixed problems pointed out. What I had posted works. With your code, try entering 21. What happens?

Sorry about that this is my first time using python forums


RE: Higher or lower game - jefsummers - May-28-2023

And AI enters the forums.....