Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
guess the number
#9
You were so close. Just need to keep the flow in mind. I've moved things around a little and used a couple of while True loops, one for replaying and the other for guessing.

I also moved the test of whether or not the guess is in range to the first rather than last test before success as a number below 1 is out of range but will also be True on the guess < n test.

https://repl.it/@gruntfutuk/MassiveScalyBluejay

import random
affirmation = ['yes', 'y', 'ok']
while True:
    n = random.randint(1, 100)
    tries = 0
    while True:
        response = input("Please enter a number from 1 to 100 (enter to exit): ")
        if not response:
            break

        if not response.isdigit():
            print("That wasn't a valid response. Please try again.")
            continue
        
        guess = int(response)
        tries += 1
        points = 100 - tries
         
        if guess < 1 or guess > 100:
            print("Number out of range. Please enter a number within range of 1 to 100")
        elif guess < n:
            print ("\nNumber is bigger than the one you typed.")
            print("Please try again. ", end='')
        elif guess > n:
            print ("\nNumber is smaller than the one you typed")
            print("Please try again. ", end='')
        else:
            print ("\nYou found it after", tries," and you won ",points," points")
            break
        
    word = input('Would you like to play again? (Yes/No): ')
    if not word.lower() in affirmation:
        break
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
guess the number - by atux_null - Dec-05-2017, 02:09 PM
RE: guess the number - by Larz60+ - Dec-05-2017, 04:36 PM
RE: guess the number - by atux_null - Dec-05-2017, 05:30 PM
RE: guess the number - by jakegold98 - Dec-05-2017, 06:35 PM
RE: guess the number - by atux_null - Dec-05-2017, 07:22 PM
RE: guess the number - by jakegold98 - Dec-05-2017, 09:01 PM
RE: guess the number - by incineratez - Dec-06-2017, 03:47 AM
RE: guess the number - by atux_null - Dec-06-2017, 06:50 AM
RE: guess the number - by gruntfutuk - Dec-06-2017, 09:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the number game jackthechampion 5 3,310 Mar-07-2020, 02:58 AM
Last Post: AKNL
  Asking for help in my code for a "Guess the number" game. Domz 5 4,001 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Guess a number game Drone4four 4 5,460 Nov-16-2018, 03:56 AM
Last Post: Drone4four

Forum Jump:

User Panel Messages

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