jedimilk
jedimilk

Reputation: 15

Python throwing invalid syntax even though syntax is correct

I've been working with arrays lately and During my practice program for some reason it just will not stop throwing a syntax error and all my IDE tells me is unexpected. As far as I can tell everything is fine. I just need another pair of eyes.

I've tried rewriting from the ground up and starting a new file entirely. I even uninstalled python and reinstalled it.

import array as arr
employee_names = arr.array("u",[])
employee_hours = arr.array("u,",[])
employee_wage = arr.array("u",[])
input_employees = int(input("Type 1 if you want to start or 0 if you want to quit: ")

while input_employees == 1:
    input_names = input("Type in the names of the employees: ")
    employee_names.append(input_names)
    input_employees = int(input("If you want to enter more press 1 or if you are done press 0: ")
    if input_employees == 0:
        break
        print(employee_names)
    else:
    continue

But when you run it you get a syntax error at the while statement for some reason.

Upvotes: 0

Views: 258

Answers (2)

abhinav
abhinav

Reputation: 687

As per the code snippet you have given the problem seems with the

else:
continue

part. Python works completely on indentation so always keep a check on your nested indentation when writing Python code. Here is the code without error:-

import array as arr
employee_names = arr.array("u",[])
employee_hours = arr.array("u,",[])
employee_wage = arr.array("u",[])
input_employees = int(input("Type 1 if you want to start or 0 if you want to quit: ")

while input_employees == 1:
    input_names = input("Type in the names of the employees: ")
    employee_names.append(input_names)
    input_employees = int(input("If you want to enter more press 1 or if you are done press 0: ")
    if input_employees == 0:
        print(employee_names) // print before break
        break
    else:
       continue

Also, else part can be omitted as the loop will continue even if else part is not added.

Upvotes: 0

R4444
R4444

Reputation: 2114

I think this should work fine:

employee_names = []
employee_hours = []
employee_wage = []
input_employees = int(input("Type 1 if you want to start or 0 if you want to quit: "))
while input_employees:
    input_names = input("Type in the names of the employees: ")
    employee_names.append(input_names)
    input_employees = int(input("If you want to enter more press 1 or if you are done press 0: "))
    if not input_employees:
        break
    else:
        continue

Please remember:

1) Always complete your parenthesis.

2) Learn the syntax before start coding.

Note: I didn't fix your algorithm, just fixed the possible errors.

Upvotes: 1

Related Questions