Beginner: Number guessing game not working

Hello! I’m a beginner at Python, I’ve finished a course about the basics regarding Python and am now attempting to do some beginner Python projects from a Youtube video. Right now I’m attempting to create a number-guessing game where the user thinks of a number and the program is supposed to guess it.
I use the IDE Pycharm for this code.

This is the video I found the project on: https://www.youtube.com/watch?v=8ext9G7xspg&list=WL&index=1&t=1036s (time: 13:17)
Even though I thought followed along with the tutorial perfectly, the code isn’t working like it does in the video. The program guesses a number, but even though I give it feedback on the number being to high, it might still guess a higher number, or the same number again. Could someone tell me what’s wrong with the code I wrote and give tips to how I could fix it?

Here is the code:

import random

def computer_guess(x):
    low = 1 
    # Lowest random number
    
    high = x
    # highest random number
    
    feedback = ""
    while feedback != "c":
        if low != high:
            guess = random.randint(low, high)
        else:
            guess = low
        feedback = (input(f"Is {guess} too high (H), too low (L) or correct(C)? ")).lower
        if feedback == 'h':
            high = guess - 1 
       # 
        elif feedback == 'l': # Lower case L
            low = guess + 1

    print(f'The computer guessed the correct number: {guess}!')

computer_guess(10)

I think the problem might simply be that you wrote .lower instead of .lower().

2 Likes

Dang, I kept checking and comparing the code over and over, yet I didn’t catch that. Now I feel silly. Thank you for the help! I guess I just need to be more observant.

There’s no need to feel silly; mistakes like that happen ALL THE TIME, no matter what your skill level is. What you should be doing is not thinking “I should have known better than that”, but “what could I have done to discover this problem?”

So, a couple of suggestions. Whenever something quirky is happening, pepper your code with prints. “If In Doubt, Print It Out” – IIDPIO – is the most fundamental form of debugging, and a vital skill to have. (Interactive debuggers are cool too, but you won’t always have one available.) In this particular case, I would be printing out low and high at each time through the loop. And then if they look right, pick something else; or if they look wrong, try to figure out why they’re wrong. You would find in this situation that they aren’t changing. WHY aren’t they changing? You were sure that you gave the feedback of "h", so surely that’s correct? Well, okay, let’s try adding else: print(repr(feedback)) in case there’s something strange going on. Maybe there’s some extra characters in the string (whitespace, or something) that means it’s comparing unequal. And that’s where you would spot it! It’s a bound method object!

Obviously this is only one possible debugging path your brain might have taken, but the idea is still the same - pick something you’re uncertain of, and print it out. Occasionally, pick something you are utterly convinced is fine, and print that out too - sometimes called a “sanity check”, because when they start failing, you start questioning your own sanity…

3 Likes

I can second what @Rosuav wrote. Python is great for fast prototyping, but because of its dynamic, untyped nature many bugs cannot be found until runtime. The original buggy code was perfectly valid Python code, and static linters and checkers (like pylint, flake8 or mypy) would also not find this bug (in this particular program). So, the only way to debug then is to somehow trace it at runtime. And print statements are the most simple way to do so.

Btw - In some cases you can also use ChatGPT to review code. In this case, if you just ask it to find the bug in the original code, and copy past that code, it will identify that issue. (Weirdly enough it also suggests to change the block

if feedback == 'h':
            high = guess - 1 

to … the identical code! It turns out that somehow the empty comment line after that block trips it up a bit… :rofl:)

Everything that I said is true in ANY language, though :slight_smile: Bugs happen, and we must learn to deal with them. Actually, Python is one of the better languages in that respect, since the tracebacks are extremely helpful.

ChatGPT is not a coding AI, and it will often suggest things that are completely and utterly wrong. It does not understand the concept of truth or correctness. In my opinion it is worse than useless for finding errors, since it makes enough of its own.

The OP did far better by asking actual Python programmers. There’s no need to turn to an AI that might make suggestions that only make it harder for us to help.

1 Like

True. For sth that is not a coding AI it performs pretty well though. And big errors are not pernicious – the worst ones are the subtle errors. But in this case it did actually find the bug - and it’s easy to check if its suggestions are ok or not.
For simple and general questions I have not seen it make many big mistakes. So, I still find it useful while learning Rust for instance - but yeah, you have to be constantly skeptical of its replies.

Yes, that’s exactly why I do not believe it is a good tool, especially for an inexperienced programmer.

Not always. If you have non-working code, and ChatGPT suggests a change, how do you know whether it’s an improvement? The best thing to do is to reject the change regardless, because that way, when humans look at your code later, they’re looking at YOUR code, not the version that ChatGPT has corrupted.

There are AIs designed for code (eg Copilot), and some of those can be useful, but ChatGPT is not one of them and should not be used for coding advice.

That’s simple - you try it out :slight_smile:

I haven’t tried out CoPilot, but last time I looked for comparative evaluations, most people were saying that its code quality was actually worse than ChatGPT…

I agree for beginners it may be a bit risky. And agree they should be warned. And it’s in principle still better to ask an experience programmer. But people also have to learn to solve things for themselves – using whatever tools are available. I also think it’s better for beginning programmers to get very familiar with these tools (several of them) at an early stage. They will be using tools like this a lot in their career - so it’s also good to become familiar with their limitations early on. – Apart from that, there may also be Python newbies who actually already know some other language or have been using these AIs in other domains, for them these kind of tools are definitely useful imo. – But I think we’re kind of getting sidetracked and you won’t be convinced by any of this :slight_smile:

Using good tools, yes. But I would not teach a beginning carpenter that a screwdriver can be used as a hammer, even though it can. Let’s not push people down a road that is counter-productive. New programmers do NOT need to be taught bad habits - they’ll learn enough of their own :slight_smile:

ChatGPT partially “agrees” when I asked it

Is it bad for beginning Python programmers to ask help of AIs like ChatGPT? What are the risks when doing so?

Under the (pretty complete) list of Cons and Risks:

Using AI to generate code without understanding it can hinder your programming growth and result in poor-quality solutions.

Under the (pretty good) tips:

Don’t hesitate to seek help from human experts or mentors when you encounter challenging issues or need guidance.

I will leave it at that :rofl: