即使语法正确,Python也会抛出无效语法

时间:2019-05-07 01:20:45

标签: python syntax

我最近一直在使用数组,在我的练习程序中,由于某种原因,它不会停止抛出语法错误,并且我的所有IDE告诉我都是意外的。据我所知,一切都很好。我只需要另一双眼睛。

我尝试从头开始重写并完全启动一个新文件。我什至卸载了python,然后重新安装。

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

但是运行它时,由于某种原因,您会在while语句中遇到语法错误。

2 个答案:

答案 0 :(得分:1)

我认为这应该可以正常工作

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

请记住:

1)务必填写括号。

2)在开始编码之前学习语法。

  

注意:我没有修复您的算法,只是修复了可能的错误。

答案 1 :(得分:0)

根据您给出的代码段

else:
continue

部分。 Python完全适用于缩进,因此在编写Python代码时请始终检查嵌套的缩进。这是没有错误的代码:-

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

此外,可以省略else部分,因为即使没有添加else部分,循环也会继续。

相关问题