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


Messages In This Thread
Higher or lower game - by SEWII - Mar-13-2023, 05:42 AM
RE: Higher or lower game - by jefsummers - Mar-13-2023, 06:43 PM
RE: Higher or lower game - by SEWII - Mar-14-2023, 03:24 AM
RE: Higher or lower game - by SEWII - Mar-14-2023, 03:25 AM
RE: Higher or lower game - by deanhystad - Mar-14-2023, 04:56 PM
RE: Higher or lower game - by SEWII - Mar-14-2023, 06:33 PM
RE: Higher or lower game - by jefsummers - Mar-14-2023, 04:57 PM
RE: Higher or lower game - by SEWII - Mar-14-2023, 06:34 PM
RE: Higher or lower game - by deanhystad - Mar-14-2023, 07:38 PM
RE: Higher or lower game - by SEWII - Mar-15-2023, 12:30 AM
RE: Higher or lower game - by jefsummers - Mar-15-2023, 01:22 PM
RE: Higher or lower game - by SEWII - Mar-15-2023, 06:31 PM
RE: Higher or lower game - by SEWII - Mar-15-2023, 06:33 PM
RE: Higher or lower game - by jefsummers - Mar-15-2023, 06:56 PM
RE: Higher or lower game - by SEWII - Mar-15-2023, 07:11 PM
RE: Higher or lower game - by deanhystad - Mar-15-2023, 07:00 PM
RE: Higher or lower game - by SEWII - Mar-15-2023, 07:10 PM
RE: Higher or lower game - by jefsummers - May-28-2023, 11:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract lower/upper antitriangular matrix schniefen 2 1,454 Dec-09-2022, 08:57 PM
Last Post: Gribouillis
  Check if all the numbers are higher than five and if not... banidjamali 3 2,150 Jan-11-2021, 11:25 AM
Last Post: banidjamali
  HELP - Returning self numbers lower or equal to my argument Kokuzuma 4 2,760 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