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?
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).
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. 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.
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.
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!
# 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…
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.
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.