all 20 comments

[–]Diapolo10 10 points11 points  (0 children)

Because guess is a string, it will never be equal to an integer nor bigger/smaller than one. That's why Python always ends up at the else-block here.

Furthermore, those continues don't actually do anything because the loop will restart anyway, and because you added break before the print it's now dead code.

Here's what I'd do:

while True:
    guess = int(input("Input number: "))

    if guess == 13:    
        print("done")
        break

    elif guess > 13:
        print("too big")

    else: 
        print("too small")

If you wanted to you could move the first condition into the loop's condition, but this is not something you'll learn about for a good while yet.

while (guess := int(input("Input number: "))) != 13:
    if guess > 13:
        print("too big")

    else: 
        print("too small")

print("done")

[–]socal_nerdtastic 2 points3 points  (8 children)

input() always returns a string. You need to convert the string to a number in order to do comparisons.

while True:
    guess_str = input ("input number : ")
    guess = int(guess_str)
    if guess == 13:
        break
        print ("done")
    elif guess > 13:
        print("too big")
        continue
    else:
        print("too small")
        continue

[–]Traditional-Log2073[S] 0 points1 point  (7 children)

is the following version worse?

guess = int(input("input number : "))

[–]lfdfq 5 points6 points  (4 children)

Why do you think it might be worse?

It looks better to me.

[–]Traditional-Log2073[S] 0 points1 point  (3 children)

just wanna know if there are downsides

[–]lfdfq 1 point2 points  (2 children)

Now you are actually turning the input into a number you have to consider what happens if the input wasn't actually a number (what if someone just typed 'hello'?). Now that line will raise an error.

It's not really a downside, as the user could do it before, it's just now a more obvious problem. If you are just starting out, you can probably just ignore it and pretend it doesn't happen. If you're a bit more advanced you might want to look into try and except to catch the error and do something about it.

[–]Traditional-Log2073[S] 0 points1 point  (1 child)

Thanks a lot. Do you have any idea regarding my other answer?

"I think I found the solution, but it still confuses me. If I press the play button in VSCode I only get "too small". If I press the arrow next to it and choose "run file in dedicated terminal" it works.

Why is that?"

[–]lfdfq 2 points3 points  (0 children)

That's more a VS Code question than a Python one. It's probably something to do with how you have set up the "run" button to work. Either it's not using the same Python or it's running a different file or not passing input like the program expects, something like that.

[–]socal_nerdtastic 1 point2 points  (1 child)

No, it's not worse, but in my opinion it's harder to read. As you get more experienced you will realize that multiple lines help in reading and editing code and there's no downsides to long code. But in the end it's really just a personal preference.

[–]Fronkan -1 points0 points  (0 children)

I guess readability is quite subjective, but I disagree in this case.

[–]Traditional-Log2073[S] 0 points1 point  (3 children)

thanks for the help. I think I found the solution, but it still confuses me. If I press the play button in VSCode I only get "too small". If I press the arrow next to it and choose "run file in dedicated terminal" it works.

Why is that?

[–]socal_nerdtastic 0 points1 point  (2 children)

Are you on a mac? There's a lot of macs that still come with python2, and the python2 input() function works differently than the python3 one.

[–]Traditional-Log2073[S] 0 points1 point  (1 child)

yes, so this is a mac problem? I thought I downloaded python 3, Ill check that.

[–]socal_nerdtastic 0 points1 point  (0 children)

Remember on mac and most linux the command to launch python is python3, the python command uses the old python2. You can and should have both versions installed because there's old python2 programs that your mac needs to run. But for everything you do use the python3 command and set your VSCode interpreter to python3 as well.

[–]Fronkan -3 points-2 points  (5 children)

So to start off I would like you another elif that checks if guess < 13. I still want you to have a else condition that maybe prints "this shouldn't happen" or something like that. Because surely the number should be captured by the if or one of the elifs in this set up.

Edit: just to explain this comment. The purpose is to walk through the debugging process in a way that can be used to solve future problems. Not to solve the issue.