Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
guess the number
#1

Hi. i am trying to make the guess the number, but i am getting an error at the end were i have to replay the game. here is my code:
import random
n = random.randint(1, 100)
tries=0
guess=0
guess = int(input("Please enter a number from 1 to 100: "))
while n != "guess":
    tries += 1
    points=100-tries
    
    if guess < n:
        print ("\nNumber is bigger than the one you typed.")
        guess = int(input("Please try again, by typing a number from 1 to 100: "))
    elif guess > n:
        print ("\nNumber is smaller than the one you typed")
        guess = int(input("Please try again, by typing a number from 1 to 100: "))
    elif guess < 1 or guess > 100:
        print("Number out of range. Please enter a number within range of 1 to 100")
    else:
        print ("\nYou found it after", tries," and you won ",points," points")
        [b]while True:
            word = input('Would you like to play again? (YES/NO): ')
            if word == 'NO':                             
                break[/b]
    print
the part of the code with bold is the one that i have issues with. I would like the user to be able to continue playing the game by typing yes or end the game by typing no. Also i would like to limit the number of tries to 10. Could someone evaluate the above code and help me please?
Reply
#2
Can't see any 'bold' code/ Please include error traceback in it's entirety instead.
Reply
#3
Hi, its that part of the code
 while True:
            word = input('Would you like to play again? (YES/NO): ')
            if word == 'NO':                             
                break
It did not show the bold inside the python code. Anyway, i am stuck with it.
Reply
#4
Hey I actually just did this in a little ATM program I made what I did was defined a function for continuing and just had that executed at the end where I wanted it to happen here is what I mean:

def another():
    answer = input("Would you like to make another transaction y/n?: ").upper()
    if answer == 'y':
        active = True
    else:
        active = False
        sys.exit("Thank you for using JTM")
you could change the wording to fit your program but that was just what I found easiest and what kept me from rewriting the code a bunch of times for different options. So an example of how I used the function is here:

while active:
    print("1: Deposite")
    print("2: Withdraw")
    print("3: Balance")

    option = int(input("What would you like to do: "))

    if option == 1:
        deposite = int(input("How much would you like to deposite: "))
        print("Your deposited $" +str(deposite))
        balance = balance + deposite
        another()
so as long as the user enters 'y' when asked to continue the loop will start over otherwise but using sys.exit() I terminate the program. By the way if you do use this method be sure to just import sys let me know if this helped you out man! I am still a beginner too but feels nice to provide some insight.
Reply
#5
Since i am at the beginning of learning python i will avoid functions.
cold someone help me correct my original code, please?
Reply
#6
I am also a beginner here but I am just saying using the function is really simple and not hard to learn I pretty much showed you what there was to it. But also you are mixing HTML with Python I think I did some research and it seems that [b] isn't really a thing in python what you could do is make the string end in .upper() which would essentially create the effect of bolding by making it all capital letters. If I come up with something I will post it but there also is a few other errors in your code you might want to look at. for example
while n != "guess":
that is saying while n is not equal to the string guess not the variable guess because it is in double quotes, instead make it

while n != guess: 
a few other issues I saw I can post about later when I look at it again.
Reply
#7
Im pretty sure the while loop for replaying the game should be moved to above the first while loop and then indent everything below it
Reply
#8
(Dec-06-2017, 03:47 AM)incineratez Wrote: Im pretty sure the while loop for replaying the game should be moved to above the first while loop and then indent everything below it

i've lost you. could you point me where please?
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the number game jackthechampion 5 3,132 Mar-07-2020, 02:58 AM
Last Post: AKNL
  Asking for help in my code for a "Guess the number" game. Domz 5 3,745 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Guess a number game Drone4four 4 5,178 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