If loops not entering

Hello, I am a beginner with python.
I am trying to make a number guessing game.

import random
D = 0
E = False
F = False

if F == True:
     A = int(input("Guessing a number between:\n"))
     B = int(input("and\n"))
if (A - B) < 0:
     C = random.randrange((A+1), B)
if (B - A) < 0:
     C = random.randrange((B+1), A)

if B == A:
     B += 2
while D != C:
     print(C)
     D = int(input("Now guess the number.\n"))
     if D == C:
          print("Congrats,  you win.")
     if (D / C) in range(-1, 2) and D != C:
          print("Close, keep guessing.")
     if A - B < 0:
          if D < A or D > B:
               print("This is not an applicable number.")
     if B - A < 0:
          if D > A or D < B:
               print("This is not an applicable number.")
     if D < C and D != C:
          print("Need to go higher.")
     if D > C and D != C:
          print("Need to go lower.")
     F = True

Here is the error I have been getting:

if (A - B) < 0:
  File "<stdin>", line 4
    if (A - B) < 0:
    ^^
SyntaxError: invalid syntax

Can anyone shed some light on why it’s flagging the loop?

This is the same as

if F:

Which means the input functions are not executed as you set to False above.

It’s very rare that comparing to True or False is required or a good thing to write.

Hello,

note that in Python, parentheses are not required. So, if (A - B) < 0 can just be if A - B < 0. You also have to assign and initialize both A and B at the top of your script with default values. Assuming F is False (its default value), both A and B will never be created and all subsequent checks of these two variables will generate an exception error (NameError).

Also, what condition changes the value of F?

But can help make code easier to read.

2 Likes

Concerning the error message that you have obtained. It looks as if you have copied the code into the console in interactive mode (REPL) of Python. Can you confirm this? In this case, the parsing is sometimes a bit odd.

So, usually the scripts are saved as a *.py file and then opened with the Python interpreter. In that case, there is no syntax error in your code. :partying_face: However, then you will obtain another error message:

NameError: name 'A' is not defined

This is because you set A only if F is True, however you set F to be False at the beginning of the script.

Hi, I noticed a few problems that might cause your code to behave in an unexpected way:

  1. Number of spaces for each indentation level

    This is not necessary, but it is widely adopted practice to use 4 or 2 spaces (or a tab character) for each indentation level.

    (Deleted - I made a mistake here)

    This is an interesting discovery for me also. Let’s paste your code into an editor which reveals spaces as “gray dots”:

    Aha, we see five spaces inside each if block! I used code formatter “black” to convert all of them into 4 spaces. And then I saw a different error being printed in my console (it was printing nothing), that leads us to the second problem.

  2. NameError: name 'A' is not defined

    In your code, you set the initial value of variable F to False. Hence the first if condition will not execute. As a result, variable A and B have never been assigned before they’re first read. I’m not 100% sure about what logic you’re pursuing, but changing the initial value of F to True makes the code run happily in my console!

Also, does the following look better for you?

# Instead of
if (A - B) < 0:
    ...
# How about this?
if A < B:
    ...

Writing them this way, you might be able to find out there is a missing condition that slips away - condition A == B is unhandled! If a naughty user inputs the same number for both A and B, the code will never attempt to generate a random number C nor will it ask for a different number.

It will go down the path leading to nowhere until it crashes when attempting to read undefined value C…

No, this does not play any role. 5 spaces is completely acceptable.

Yes you’re right. I’ve confirmed that the code does work with 5 spaces. I somehow cannot reproduce the behavior when it printed nothing. I am looking into that.

I inspected OP’s code block with Chrome devtools, no special character was found in the code.

That leaves only one possibility on my desk - I forgot to save the file before running it in my terminal… :clown_face:

Don’t wast your time. It is as I wrote yesterday. If you copy the code verbatim into the old REPL (not the PYREPL from Python 3.13), then you get a syntax error as in the original post.

1 Like