Skip to content

Keep receiving syntax errors in series of conditionals, beginner Python #21471

Answered by airtower-luna
retrohp asked this question in General
Discussion options

You must be logged in to vote
retrohp:
    if x_str = str(x) :

A conditional must contain a condition (a value that is True or False, or can be converted to one of those). An assignment is no condition, so you get a syntax error.

You always need to assign a variable before you can use it. A variable that hasn’t been assigned doesn’t exist, that’s why you get an error saying it’s undefined.

I’m not sure why you’re trying to do an assignment in the if condition here, it looks the thing you put in the body of the conditional is what you actually want to be the condition. If you want to assign x_str and then compare its slices, do this:

    x_str = str(x)
    if x_str[2:6] == x_str[5:1:-1]:
        # Do what should …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants