【问题标题】:Having a Issue with program not working correctly in python程序在 python 中无法正常工作的问题
【发布时间】:2021-03-08 10:46:37
【问题描述】:
       empmangpro = True
       employees = []
       while empmangpro == True:
             try:
                 empcount = len(employees)
                 print("--------------------Employee Management System--------------------")
                 print("")
                 print("There are "+str(empcount)+" employees in the system.")
                 print("")
                 print("------------------------------------------------------------------")

                 print("1. Add new employee")
                 print("2. View all employees")
                 programselection = int(input("Please select your option number: "))

                 if programselection == 1:
                 employee = []
                 employee.append(input("First and Last Name: "))
                 employee.append(input("Social Security Number: "))
                 employee.append(input("Phone Number: "))
                 employee.append(input("Email Address: "))
                 employee.append(input("Salary:$"))
                 employees.append(employee)
                 elif programselection == 2:
                      i = 0
                      j = 0
                      empstr = ""
                      while i < int(empcount):
                          while j < 5:
                             empstr = empstr + employees[i][j]
                      if j != 4:
                         empstr = empstr + ", "
                         j += 1
                         if i+1 != int(empcount):
                            empstr = empstr + "\n"
                            j = 0
                            i += 1
                           print(empstr)
                           print[employees]
                        elif programselection < 3:
                           empmangpro = False
                        else:
                             print("Please enter valid information")
                except ValueError:
                print("Please enter valid information")
                continue

我有选项 1,您可以将多个员工添加到系统中,但是当我选择选项 2 时,没有任何反应。它应该打印我添加的所有员工。我在这里做错了什么?我编程才不到一个月,所以我还有很多东西要学。我错过了什么或做错了什么?

【问题讨论】:

  • 在您的if programselection == 1: 之后,您是否缩进下一行?现在看来没有。 elif 也应与 if 处于同一级别。
  • 我在发布此内容时弄乱了间距我没有收到错误或任何内容,只是当我选择选项 2 时没有任何反应,它只是空白
  • 您要求我们用一种可以改变程序控制的语言(例如 python)来调试您的间距不正确的代码。我们怎么知道它是复制意大利面还是实际问题(隐藏)。
  • 嘿@Co​​rey 对不起,如果我觉得我很粗鲁。我想帮助你,并要求你努力正确地格式化你的代码块。
  • 没关系,我想我现在得到了正确的间距?

标签: python loops error-handling


【解决方案1】:

不太清楚您在选项 2 中尝试做什么。请尝试在将来评论您的代码。你帖子中的标签不准确,所以我做了一些猜测。也许这会帮助你解决你的问题:

empmangpro = True
employees = []
while empmangpro == True:
    try:
        empcount = len(employees)
        print("--------------------Employee Management System--------------------")
        print("")
        print("There are "+str(empcount)+" employees in the system.")
        print("")
        print("------------------------------------------------------------------")

        print("1. Add new employee")
        print("2. View all employees")
        programselection = int(input("Please select your option number: "))

        if programselection == 1:
            employee = []
            employee.append(input("First and Last Name: "))
            employee.append(input("Soci1al Security Number: "))
            employee.append(input("Phone Number: "))
            employee.append(input("Email Address: "))
            employee.append(input("Salary:$"))
            employees.append(employee)
        elif programselection == 2:
            i = 0
            j = 0
            empstr = ""
            while i < empcount:
                print(str(employees[i])+"\n")
                i += 1

        elif programselection > 2:
            empmangpro = False
            print("Stopping programm")
        else:
            print("Please enter valid information")
    except ValueError:
        print("Please enter valid information")
        continue

此外,如果您想停止程序,请使用 elif programselection &gt; 2: 而不是 elif programselection &lt; 3:

【讨论】:

    【解决方案2】:

    您说选项 1 有效。好的(我看到一些缩进问题)。

    对于选项 2,老实说,我并没有完全遵循您的代码。如果您希望选项 2 打印所有员工,我建议创建一个 Employee 类。这样,您可以在任何地方print(employee)

    可以把这个放到employee.py:

    class Employee:
        def __init__(self, name): # can add all you need social security, ...
            first, last = name.split(' ')
            self.first = first # add a new line per new parameter
            self.last = last
        def __str__(self):
            # here you can add all of your logic
            # to properly format social security, phone number, etc.
            # just be sure to return a string, and not print a string!
            return f'{self.last}, {self.first}'
    

    然后在你的主文件中:

    import employee # put at top of file
    employees = [Employee('John Doe'), Employee('Jane Doe')] # sample data
    for e in employees:
        print(e)
    

    输出:

    Doe, John
    Doe, Jane
    

    至于不正确的间隔:

                 if programselection == 1:
                     employee = []
                     # ...
                     employees.append(employee)
                 elif programselection == 2:
    

    还有:

                except ValueError:
                    print("Please enter valid information")
                    continue
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多