Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Higher or lower game
#11
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()
Reply
#12
thanks
Reply
#13
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
Reply
#14
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?
SEWII likes this post
Reply
#15
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")
Reply
#16
(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
Reply
#17
(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
Reply
#18
And AI enters the forums.....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract lower/upper antitriangular matrix schniefen 2 1,374 Dec-09-2022, 08:57 PM
Last Post: Gribouillis
  Check if all the numbers are higher than five and if not... banidjamali 3 2,059 Jan-11-2021, 11:25 AM
Last Post: banidjamali
  HELP - Returning self numbers lower or equal to my argument Kokuzuma 4 2,687 Nov-01-2018, 06:35 PM
Last Post: Kokuzuma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020