Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Higher or lower game
#1
Assist in coding to create higher or lower game.
Display the screen to enter users name and showing a welcome 'banner'
After the user enters there name
Ask the player to a guess a number between 1 - 20
which then has 5 attempts to guess the number
user guess and enter random number
Logic
If user answer > Random imported number
reply = Your guess is to high the answer is lower
elif user answer < Random imported number
Reply = Your guess is to low the answer is higher
elif user answer == random imported number
reply = Well done your answer is correct
while also deducting a guess each time
else if user runs out of guess
reply = unlucky your out of guesses
break
Then ask the user if he wants to play again
if yes then play again
if no Break
Then display a goodbye 'banner'
Yoriz write Mar-13-2023, 06:23 AM:
Moved to homework:
https://python-forum.io/misc.php?action=help&hid=52 Wrote:Homework and No Effort Questions
This forum is focused on education. It exists to help people learn Python. We don’t exist to solve others' problems.
Reply
#2
What have you got so far, and where are you stuck (or what errors are you getting)?
Reply
#3
(Mar-13-2023, 06:43 PM)jefsummers Wrote: What have you got so far, and where are you stuck (or what errors are you getting)?

This is what I have so far

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("| ----------------------------------------- |")

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")
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"]
correct_comment = random.choice(correct)
wrong_comment = random.choice(wrong)
play = "y"
guess = 5
while play == "y":
    pc_choice = random.randint(1,20)
    print(pc_choice)
    while True:
            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")

    if answer > pc_choice:
        print("your guess is to high guess lower")
        guess = guess -1
        print(f"you have {guess} guesses left")
    elif answer < pc_choice:
        print("your guess is to low guess higher")
        guess = guess -1
        print(f"you have {guess} guesses left")
    elif answer == pc_choice:
        print(f"{correct_comment}")
    elif guess == 0:
        print(f"{wrong_comment}{pc_choice}")
        break
        goodbye_banner
    else:
        break

while True:
    again = input("do you want to play again? enter y to play again or n to quit")
    if again == ("n"):
        goodbye_banner()
        break
Larz60+ write Mar-14-2023, 03:43 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#4
The problem is that is shows the answer already and that every time the user guesses an answer the number changes.
It also doesn't end when the person runs out of guesses it just keeps going.
Reply
#5
If you don't want to show the answer, why do you print it?

Your other error is a bit more subtle. After you generate a random number you want to give the player multiple tries to guess the correct number. That means you need a loop after you generate the random answer. Your code also does some input verification that uses a loop. That would be inside the guessing loop. The overall layout is like this:
display welcome banner
WHILE PLAYING
    GENERATE RANDOM NUMBER
    WHILE GUESSING
        WHILE VERIFYING
            VERIFY INPUT
        CHECK IF INPUT IS CORRECT
    ASK IF PLAY AGAIN
display thanks for playing banner
You have most of these code pieces except the guessing loop, and some of your pieces aren't in the right place. For example, your ask if play again code needs to be inside the WHILE playing loop.

You could write this as a bunch of nested loops, but it would be better organized as a group of functions
def get_guess():
    """ Get user's guess """
   ...

def play_game():
    """ Play the guessing game """
    ...

def play_again():
    """ Returns True if user wants to play again """
    ...

print(welcome_banner)
while True:
    play_game()
    if not play_again():
        break
print(thanks_for_playing_banner)
    
Reply
#6
You need to organize the code better, suggest
Initialization (imports, constants, player name, etc.)
Multi-game loop (allow for new games)
Game loop (initialize what you need for each game - the target number, set guess_no to 5, etc.)
Guess loop (get the guess, compare to target, respond appropriately. Make sure that guess_no is decremented and when reaches 0 exit out to the multi-game loop)

Also, around line 56 you call goodbye_banner without perentheses, so that does nothing.
Your whole if/elif/else structures are messy. Most of your break commands are not necessary.
Reply
#7
(Mar-14-2023, 04:56 PM)deanhystad Wrote: If you don't want to show the answer, why do you print it?

Your other error is a bit more subtle. After you generate a random number you want to give the player multiple tries to guess the correct number. That means you need a loop after you generate the random answer. Your code also does some input verification that uses a loop. That would be inside the guessing loop. The overall layout is like this:
display welcome banner
WHILE PLAYING
    GENERATE RANDOM NUMBER
    WHILE GUESSING
        WHILE VERIFYING
            VERIFY INPUT
        CHECK IF INPUT IS CORRECT
    ASK IF PLAY AGAIN
display thanks for playing banner
You have most of these code pieces except the guessing loop, and some of your pieces aren't in the right place. For example, your ask if play again code needs to be inside the WHILE playing loop.

You could write this as a bunch of nested loops, but it would be better organized as a group of functions
def get_guess():
    """ Get user's guess """
   ...

def play_game():
    """ Play the guessing game """
    ...

def play_again():
    """ Returns True if user wants to play again """
    ...

print(welcome_banner)
while True:
    play_game()
    if not play_again():
        break
print(thanks_for_playing_banner)
    
Where would I add the guessing loop and what type of loop should I use
Reply
#8
(Mar-14-2023, 04:57 PM)jefsummers Wrote: You need to organize the code better, suggest
Initialization (imports, constants, player name, etc.)
Multi-game loop (allow for new games)
Game loop (initialize what you need for each game - the target number, set guess_no to 5, etc.)
Guess loop (get the guess, compare to target, respond appropriately. Make sure that guess_no is decremented and when reaches 0 exit out to the multi-game loop)

Also, around line 56 you call goodbye_banner without perentheses, so that does nothing.
Your whole if/elif/else structures are messy. Most of your break commands are not necessary.
How do I exactly fix it could you give me an example or do some of it.
Or give me a step by step tutorial if possible
Reply
#9
Output:
Where would I add the guessing loop and what type of loop should I use
The guessing loop would be in the function that plays a single game. What type of loop is completely up to you. Is there a limit on how many guesses you can make?
Reply
#10
(Mar-14-2023, 07:38 PM)deanhystad Wrote:
Output:
Where would I add the guessing loop and what type of loop should I use
The guessing loop would be in the function that plays a single game. What type of loop is completely up to you. Is there a limit on how many guesses you can make?

The max is 5 guesses
Reply


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