Mar-15-2023, 01:22 PM
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.
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()