Python Forum
Play again, in a guessing game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Play again, in a guessing game
#1
Question 
Hello!

Here the computer comes up with a number in range(1, 9), and we try to guess it, until we come up with the correct number.
We can exit anytime during the game and also at the end we have the choice to play again.
import random
def guessing():
    computer = random.randint(1, 9)
    number_of_guesses = 0
    while True:
        number_of_guesses += 1
        pick = input("Guess, if you dare: ")
        if pick.lower() == "exit":
            print("Better luck next time!")
            print("Attempts:", number_of_guesses - 1)
            break
        elif int(pick) > computer:
            print("Too high.")
        elif int(pick) < computer:
            print("Too low.")
        elif int(pick) == computer:
            print("Yes!")
            if number_of_guesses == 1:
                print("Are you God?!")
            elif number_of_guesses < 3:
                print(f"Impressive! Only {number_of_guesses} attemps.")
            else:
                print(f"Finally! After {number_of_guesses} attemps.")
            break
    again()

def again():
    if input("Continue?(y/n) ") == 'y':
        guessing()
    else:
        exit

guessing()
I've been told that I don't need to define a separate function for just asking the user if they want to play again. But I can't seem combine the two functions together. Wall

Thank you for reading.
Reply


Messages In This Thread
Play again, in a guessing game - by banidjamali - Jan-20-2021, 08:03 AM
RE: Play again, in a guessing game - by buran - Jan-20-2021, 08:52 AM
RE: Play again, in a guessing game - by deanhystad - Jan-20-2021, 11:42 AM
RE: Play again, in a guessing game - by snippsat - Jan-20-2021, 06:38 PM
RE: Play again, in a guessing game - by banidjamali - Jan-22-2021, 06:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help - random number/letter guessing game juin22 1 3,260 Aug-16-2020, 06:36 AM
Last Post: DPaul
  making a guessing number game blacklight 1 2,248 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  An interesting Role-Play-Game battle script NitinL 4 3,515 Apr-02-2020, 03:51 AM
Last Post: NitinL
  Guessing game with 4 different digits LinseyLogi 6 3,745 Mar-29-2020, 10:49 AM
Last Post: pyzyx3qwerty
  Guessing game with comparison operators Drone4four 9 13,943 Dec-02-2018, 06:12 PM
Last Post: ichabod801
  Guessing Game "limit 4 attempts" help rprollin 3 19,951 Jun-23-2018, 04:37 PM
Last Post: ljmetzger
  Name guessing game in python Liquid_Ocelot 6 15,162 Apr-01-2017, 12:52 PM
Last Post: ichabod801
  guessing script simon 3 5,695 Oct-10-2016, 01:47 PM
Last Post: simon
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 7,018 Oct-06-2016, 12:49 AM
Last Post: tinabina22

Forum Jump:

User Panel Messages

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