gpt4 book ai didi

循环中的Python错误

转载 作者:行者123 更新时间:2023-11-28 21:26:57 24 4
gpt4 key购买 nike

我解决了间距问题,并纠正了其他错误。现在它正在做我想做的事,但我选择了选项 2,它将打印出员工的记录 4 次。如果我输入另一个员工,它只会打印第二个而不是第一个。

class EmployeeClass:
def Employee(name, lastName, age, salary):
name = name
lastName = lastName
age = age
salary = salary
def displayEmployee(x):
print("Name: " + name + ", " + lastName)
print("Age: " + age)
print("Salary: " + salary)

EmployeeArray = []

Continue = True
print ("Employee Information V2.0")

while Continue == True:
print ("Welcome to Employee Information")
print ("1: Add New Record")
print ("2: List Records")
print ("3: Quit")

choice = input()

if choice == "1":
name = input ("Enter First Name: ")
EmployeeArray.append(name)
if name == "":
Continue = False
print ("Goodbye!")
break
lastName = input ("Enter Last Name: ")
EmployeeArray.append(lastName)
age = input ("Enter Age: ")
EmployeeArray.append(age)
salary = input ("Enter Salary: ")
EmployeeArray.append(salary)
elif choice == "2":
for Employee in EmployeeArray:
EmployeeClass.displayEmployee(Employee)
Continue = False
elif choice == "3":
print ("Bye!")
break
else:
print ("Please choose a valid option")
print ("\n")

最佳答案

您的错误消息将指示发生此问题的行号。基本上,您混合使用制表符和空格,因此您只需要一致地使用其中一个来进行缩进。

PEP8 - The Style Guide for Python建议使用 spaces 并且还注意到:

When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

通过简要检查您的来源,似乎在 print 语句前面有制表符 - 用空格替换它们(这也是它们在上面的帖子中未正确呈现的原因)

while Continue == True:
print ("Welcome to Employee Information")
print ("1: Add New Record")
print ("2: List Records")
print ("3: Quit")

可能还有其他地方,你得仔细检查。事实上,我怀疑你的代码在你的帖子中没有正确缩进的地方可能值得一看。

为了避免这样的问题,最好使用一个编辑器或 IDE,它会始终为您缩进相同的字符。

关于循环中的Python错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11954672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com