Invalid syntax: Python


#1

Im supposed to get the total number of bugs collected for 7 days.
my variable total_number_bugs = total_number_bugs + number_bugs_collected results in a syntax error.

def main():

    #define variables
    total_days = 7
    total_number_bugs = 0

    for current_day in range(1, 8):
        number_bugs_collected = eval(input("How many bugs did you collect on day " + str(current_day ))

        total_number_bugs = total_number_bugs + number_bugs_collected

print("The total number of bugs you collected for all 7 days was", total_number_bugs )

#2

here:

print("The total number of bugs you collected for all 7 days was", total_number_bugs )

total_number_bugs isn’t defined, it only exist in the main function (variable scope)


#3

I thought I defined it by giving it a value total_number_bugs = 0
total_number_bugs = total_number_bugs + number_bugs_collected


#4

mismatched braces

also, eval isn’t all that appropriate for converting string to int
sensible: multiply each digit by its positional value
not sensible: execute as code

additionally, your error message doesn’t say that your variable is the cause of the syntax error, it is saying where python’s code parser was reading when it could no longer match your code to defined patterns


#5

I’ll keep trying to figure out this code


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.