Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help for guessing game code
#1
i am very new to python and began a few days ago, and decided to build a guessing game. i have completed the game but need help in the end part. here is my code
import random
print ("Hello! Lets begin the Guessing Game!")

n = random.randint(1, 100)
print("Start by guessing the number I chose. It is between 1 and 100.")
guess = int(input())
while guess != n or guess == n:
    if guess < n:
        print('Higher')
        guess = int(input())
    elif guess > n:
        print ('Lower')
        guess = int(input())
    if guess == n:
        print ("Congrats! you got it!")
        print ('Do you want to try again')
        ans=(input(Y/N))
    if ans== N:
        raise SystemExit
    else ????
the question mark is where i am stuck. i want code that can send the user back up to the start and restart the game if they say yes. But due to an absence of goto I don't know how to do this. Thanks in advance
Reply
#2
Could simplify this a bit.
Like line 6 could be in the loop,the just lower and higher til number is guessed.
The it can be like this,can also trow in tries to.
import random

print("Start by guessing the number I chose. It is between 1 and 100.")
secret_number = random.randint(1, 100)
guess, tries = 0, 0
while guess != secret_number:
   guess = int(input("Take a guess: "))
   if guess > secret_number:
       print ("Lower")
   elif guess < secret_number:
       print ("Higher")
   tries += 1
print(f'You guessed it! The number was {guess} in {tries} tries')
Kronos Wrote:i want code that can send the user back up to the start and restart the game if they say yes. But due to an absence of goto I don't know how to do this. Thanks in advance
Yes this part can be hard to figure out,as goto is not a good idea Hand

The way to solve is using loop structure and can also use function to separate it,so it get cleaner than trying to all in one loop(how do i start at begging again Think)
Here is a demo skeleton of one way to do this,so have one menu that start the game and can quit out.
Always fall back into this menu,where can start new game or quit.
import time

def my_game():
    print('Game running')
    time.sleep(5)
    print('Game score is 42')
    input('Push enter to retun to menu')

def menu():
   while True:
       print('(1) Play game')
       print('(Q) Quit')
       choice = input('Enter your choice: ').lower()
       if choice == '1':
           my_game()
       elif choice == 'q':
           return False
       else:
           print(f'Not a correct choice: {choice},try again')

if __name__ == '__main__':
   menu()
Reply
#3
(Mar-09-2020, 12:59 PM)snippsat Wrote: Yes this part can be hard to figure out,as goto is not a good idea

The way to solve is using loop structure and can also use function to separate it,so it get cleaner than trying to all in one loop(how do i start at begging again )
Here is a demo skeleton of one way to do this,so have one menu that start the game and can quit out.
Always fall back into this menu,where can start new game or quit.
Thanks for suggesting. Can you please explain the code so i can do it myself too. Thank you very much
Reply
#4
The menu system that i posted is working code so try to run code.
Enter some wrong values and see what's happens,start the game and see it run 5-sec and that you fall back into menu.

So the my_game() could have the content of guess the number code.
Try place it in there and try figure out how this work.
I have problem with this post back,then can show demo with menu and the guess number code.
Reply
#5
(Mar-09-2020, 01:50 PM)snippsat Wrote: So the my_game() could have the content of guess the number code.
understood. i tried it and the result was perfect. Thank You SO much.
heres my finalised code.
import random
 
def my_game():
    print("Start by guessing the number I chose. It is between 1 and 100.")
    secret_number = random.randint(1, 100)
    guess, tries = 0, 0
    while guess != secret_number:
       guess = int(input("Take a guess: "))
       if guess > secret_number:
           print ("Lower")
       elif guess < secret_number:
           print ("Higher")
       tries += 1
    print('You guessed it! The number was', guess, 'in', tries,' tries')
    input('Push enter to retun to menu')
 
def menu():
   while True:
       print('(1) Play game')
       print('(Q) Quit')
       choice = input('Enter your choice: ').lower()
       if choice == '1':
           my_game()
       elif choice == 'q':
           return False
       else:
           print(f'Not a correct choice: {choice},try again')
 
if __name__ == '__main__':
   menu()
Reply
#6
Look good Smile
Function is great first way to structure code,as code should be like black box inside each function.
Of course using class is an other way structure code,i do like to use class and more OOP way to structure code to,but the simplicity of functions is also great.

So as demo could just add first function as well,as mention it will not mess up other code as function keep stuff separate.
import time
import random

def guess_game():
    print("Start by guessing the number I chose. It is between 1 and 100.")
    secret_number = random.randint(1, 100)
    guess, tries = 0, 0
    while guess != secret_number:
       guess = int(input("Take a guess: "))
       if guess > secret_number:
           print ("Lower")
       elif guess < secret_number:
           print ("Higher")
       tries += 1
    print(f'You guessed it! The number was {guess} in {tries} tries')

def annoing_game():
    print('Game running')
    time.sleep(15)
    print('Game score is 42')
    input('Push enter to retun to menu')

def menu():
   while True:
       print('(1) Play guess the number')
       print('(2) Play annoning wait game')
       print('(Q) Quit')
       choice = input('Enter your choice: ').lower()
       if choice == '1':
           guess_game()
       elif choice == '2':
           annoing_game()
       elif choice == 'q':
           return False
       else:
           print(f'Not a correct choice: {choice},try again')

if __name__ == '__main__':
   menu()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  needing some help to write some code for a game calculator rymdaksel 1 401 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  Problem with my pong game code Than999 8 3,839 May-15-2022, 06:40 AM
Last Post: deanhystad
Question Beginner Boolean question [Guessing game] TKB 4 2,296 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Unable to count the number of tries in guessing game. Frankduc 7 1,901 Mar-20-2022, 08:16 PM
Last Post: menator01
  Music Selection Game Code WyattH 3 2,429 Jul-20-2020, 11:16 AM
Last Post: ndc85430
  Guessing game problem IcodeUser8 7 3,618 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,740 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Python Help - Guessing Game JamieT 5 2,764 Apr-16-2020, 01:30 PM
Last Post: deanhystad
  Guessing game kramon19 1 2,170 Mar-25-2020, 04:17 AM
Last Post: deanhystad
  guessing the number game go127a 6 4,837 Apr-27-2019, 01:23 PM
Last Post: go127a

Forum Jump:

User Panel Messages

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