Python Forum
I don't know what's wrong with this code [Python Console]
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't know what's wrong with this code [Python Console]
#1
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.')
Reply
#2
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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.
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  In Console,Python anna17 0 134 Mar-23-2024, 08:24 PM
Last Post: anna17
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 787 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,469 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,179 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,680 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,386 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Python Flask Realtime system printout (console) ffmpeg jttolleson 3 2,859 Apr-18-2022, 06:39 PM
Last Post: jttolleson
  Wrong code in Python exercise MaartenRo 2 1,486 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,599 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983

Forum Jump:

User Panel Messages

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