Python Forum

Full Version: I don't know what's wrong with this code [Python Console]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I'm working through some school material for Higher Computer Science (I'm starting the subject late, with no prior knowledge, so please forgive any amateurish oversight/lack of knowledge). Basically the task I'm doing is simple; making a number-guessing game using a while loop. Anywayyyyyy, the problem I'm facing is that whenever the code hits my 'else' criteria for the loop (in other words, when I guess the right number), nothing happens. I'm just left unable to input anything, do anything, etc.. Could someone please have a look at my code and tell my why this is happening? I should specify, I'm using the Python 3.8 console, not the interpreter. Also, I've put (tab) where I have a tab indent, as it doesn't show in the preview where things are indented.

attempts=0;guess=0;number=random.randint(1,100)
while guess!=number:
... (tab)if int(attempts)<1:guess=input('Feeling lucky? Take a guess!\n');attempts+=1
... (tab)elif int(guess)>int(number):guess=input('Too high! Would you like to try again?\nEnter a number = Yes Alt + F4 = No\n');attempts+=1
... (tab)elif int(guess)<int(number):guess=input('Try a bit higher! Would you like to try again?\nEnter a number = Yes Alt + F4 = No\n');attempts+=1
... else:
... (tab)if int(attempts)<5:print('Congratulations! That only took you '+str(attempts)+' attempts - pretty impressive!')
... (tab)elif int(attempts)<10:print('It took you '+str(attempts)+' tries, but you got there eventually!')
... (tab)else:print('Gosh! '+str(attempts)+' guesses?! It\'s not your lucky day.')
It's really hard to tell from that code. Please checkout these instructions for using BBCode to post code. Also, please don't use ; to put multiple commands on one line. It makes it much harder to follow your code.

It's not clear to me exactly what the problem is, given how the code was posted. I would note that attempts is an integer, so you don't need to keep using int(attempts) when checking it. On the other hand, guess is a string, so you may want to use int(guess) in the while condition. Generally, I would just convert it once, either by putting int() around the input statement, or with something like guess = int(guess).
I think you must now be busy formatting your code Smile. Meanwhile you may think about the next:
(Dec-22-2019, 06:37 PM)H0M1C1D4L_P1G Wrote: [ -> ]whenever the code hits my 'else' criteria for the loop (in other words, when I guess the right number)
But as far as I can see it is the "else" of if int(attempts)<1:. So else will be executed if int(attempts) >= 1. I understand that is not what you intended.
The thing is, the else isn't listed with a tab, so he might have it associated with the while statement. In that case it should trigger once when the loop is finished. But then if he's made a guess and it's correct, the loop will never finish, because none of the if/elif conditions will be met, and the conversion problem on the while statement will keep the loop from stopping.
Okay so I have managed to resolve the problem; I just changed some of the typecasting and such, because I had forgotten that when a variable's value is determined by an input, it is automatically regarded as a string. Here is the finished code:
>>> attempts=0;guess=0;number=random.randint(1,100)
>>> while int(guess)!=number:
...     if int(attempts)<1:guess=input('Feeling lucky?  Take a guess!\n');attempts+=1
...     elif int(guess)>number:guess=input('Too high!  Would you like to try again?\nEnter a number = Yes          Alt + F4 = No\n');attempts+=1
...     elif int(guess)<number:guess=input('Try a bit higher!  Would you like to try again?\nEnter a number = Yes          Alt + F4 = No\n');attempts+=1
... else:
...     if attempts<5:print('Congratulations!  That only took you '+str(attempts)+' attempts - pretty impressive!')
...     elif int(attempts)<10:print('It took you '+str(attempts)+' tries, but you got there eventually!')
...     else:print('Gosh!  '+str(attempts)+' guesses?!  It\'s not your lucky day.');print(input('How long did you spend trying?\n')+'?  Lol')
Also, ichabod801, when you say not to use ; to put multiple commands in one line, where I have lines like
if int(attempts)<1:guess=input('Feeling lucky?  Take a guess!\n');attempts+=1
I was unsure how to put the
attempts+=1
section in a separate line with accurate syntax. Could you give me an example of how to do this? Also any optimisation (or general) tips would be appreciated.
I made a cleaned up version of your code:

attempts=0
guess=0
number=random.randint(1,100)
while guess!=number:
    if attempts<1:
        guess=input('Feeling lucky?  Take a guess!\n')
        attempts+=1
    elif guess>number:
        guess=input('Too high!  Would you like to try again?\nEnter a number = Yes          Alt + F4 = No\n')
        attempts+=1
    elif guess<number:
        guess=input('Try a bit higher!  Would you like to try again?\nEnter a number = Yes          Alt + F4 = No\n')
        attempts+=1
    guess = int(guess)
else:
    if attempts<5:
        print('Congratulations!  That only took you '+str(attempts)+' attempts - pretty impressive!')
    elif int(attempts)<10:
        print('It took you '+str(attempts)+' tries, but you got there eventually!')
    else:
        print('Gosh!  '+str(attempts)+' guesses?!  It\'s not your lucky day.')
        print(input('How long did you spend trying?\n')+'?  Lol')
By limiting it to one statement per line, the structure of your code becomes much clearer. It makes it easier for people to help you, and that makes it more likely that they will.

I also took out the unnecessary int() calls for attempts, and changed it so that there is only one int() call for guess.
Can also trow in string formatting f-string,
then it look better than doing convert with str and concatenate with +.
import random

attempts = 0
guess = 0
number = random.randint(1, 100)
while guess != number:
    if attempts < 1:
        guess = input("Feeling lucky?5 Take a guess!\n")
        attempts += 1
    elif guess > number:
        guess = input("Too high! Would you like to try again?\nEnter a number = Yes Alt + F4 = No\n")
        attempts += 1
    elif guess < number:
        guess = input("Try a bit higher! Would you like to try again?\nEnter a number = Yes Alt + F4 = No\n")
        attempts += 1
    guess = int(guess)
else:
    if attempts < 5:
        print(f"Congratulations! That only took you {attempts} attempts - pretty impressive!")
    elif int(attempts) < 10:
        print(f"It took you {attempts} tries, but you got there eventually!")
    else:
        print(f"Gosh! {attempts} guesses?!  It's not your lucky day.")
        print(input("How long did you spend trying?\n") + "?  Lol")