Skip to content Skip to sidebar Skip to footer

Python Invalid Syntax If Statement

I'm trying to make a quiz in python but I keep getting invalid syntax errors. #This is for addition questions. if (question=add) <---- That is where i get the error for i in r

Solution 1:

for comparasion you need to use ==:

if question==add:   #you dont need bracket here# do your stuff 

sum is built-in sum function dont use it for variable name

Solution 2:

Replace with if question == add: and indent the rest of your code.

Also, don't use sum as variable name. It is a useful function.

if question == add:
    for i inrange(10):
        first_number_add = random.randint(1,50)
        second_number_add = random.randint(1,50)
        answer = int(input(str(first_number_add) + '+' + str(second_number_add) + '='))
        the_sum = first_number_add + second_number_add
        if answer == the_sum:
            print('Correct')
            stats['correct'] += 1else:
            print('Incorrect.')
            stats['incorrect'] += 1

Solution 3:

if question==add:  
    for i inrange(0,10):
        first_number_add=random.randint(1,50)
        second_number_add=random.randint(1,50)
        answer=int(input(str(first_number_add) + '+' + str(second_number_add) + '='))
        sum=first_number_add+second_number_add
        if answer==sum:
            print ('Correct')
            stats['correct'] += 1else:
            print ('Incorrect.')
            stats['incorrect'] += 1

You don't need parantheses. Also you have to put 4 spaces in indent after for loop and if statement. As @Joel comment, 4 spaces is standard, you can change it to 8-16 etc.

Solution 4:

if question==add:  
    for i inrange(0,10):
        first_number_add=random.randint(1,50)
        second_number_add=random.randint(1,50)
        answer=int(input(str(first_number_add) + '+' + str(second_number_add) + '='))
        sum=first_number_add+second_number_add
        if answer==sum:
            print ('Correct')
            stats['correct'] += 1else:
            print ('Incorrect.')
            stats['incorrect'] += 1

No need of parenthesis in python... and please keep in mind to provide a consistent indentation(same number of spaces) with every block and looping statement.

Post a Comment for "Python Invalid Syntax If Statement"