将输入存储在数组中,使用哨兵值退出循环,显示一些统计数据 Python

问题描述 投票:0回答:1

我必须在退出循环之前按 0 两次,我需要打印平均工资和 5k 以内的所有工资。帮助新手?!

#此程序将员工姓名后跟他们的薪水作为输入 #使用标记值,用户可以继续输入姓名和薪水,直到 #按 0 退出。然后它应该打印平均工资和数组的内容。

#我的代码很草率。我会接受改进建议。谢谢!

from statistics import mean
import sys

names = []
salaries = []
e = None
s = None
running = True


def Main():
    e = None
    s = None
    count = 1
    while running == True:
        e = input(f"Enter Employee {count} Name      (press 0 to quit):  ")
        s = float(input(f"Enter Employee {count} salary (in hundreds)      (press 0 to quit):  "))
        L = [e, s]
        if e == 0 or s == 0:
            print(names)
            sys.exit()

       # print(statistics.mean(names))
        count = count + 1
       # print(statistics.mean(namees.append(L)))
        salaries.append(L)

主要()

python user-input sentinel
1个回答
0
投票

可能是我的方法不是100%专业,还是保留你的sys.exit()。我假设您在代码中的某处将

running
设置为 true,如果是这样,请删除我在函数中的第一行。

def Main():

    running = True
    e = None
    s = None
    count = 1
    while running == True:
        e = input(f"Enter Employee {count} Name      (press 0 to quit):  ")
        s = float(input(f"Enter Employee {count} salary (in hundreds)      (press 0 to quit):  "))
        L = [e, s]
         
        # e is str
        if e == '0' or s == 0:
            print(names)

            # this will break while loop
            running = 0 # or False
© www.soinside.com 2019 - 2024. All rights reserved.