简体   繁体   English

如何在整数范围之间打印字符串-Python 3

[英]How To Print Strings Between Integer Ranges - Python 3

I'm very new to Python and programming in general, so excuse me if the code is terrible and the problem rather easy to solve. 我一般对Python和编程都不熟悉,所以如果代码很糟糕并且问题很容易解决,请原谅。

I have written code to allow a user to have employee data printed based on 3 different inputs, which they are allowed to choose from. 我编写了代码,以允许用户根据3种不同的输入来打印员工数据,并允许他们选择。 The options the user has available to them are to pick employees based on their payroll number; 用户可以使用的选项是根据他们的工资单编号选择员工。 a minimum and maximum salary range; 最低和最高薪金范围; their job title. 他们的职称。

I made two functions for the formatting. 我为格式化设置了两个功能。 The first one turns the lines of the text file into lists, then the second function grabs those individual lists and formats them. 第一个将文本文件的行转换为列表,然后第二个函数获取这些单独的列表并设置其格式。

Then the code requests the user to input the file name. 然后代码要求用户输入文件名。 If the file cannot be found, they get to try again. 如果找不到该文件,他们可以重试。 If it is correct, the file is loaded and then runs through the functions to print out a neat table. 如果正确,将加载该文件,然后运行这些函数以打印出整洁的表格。

Then the user is asked what method they want to choose from to select specific employees. 然后,询问用户要从哪种方法中选择特定雇员。 They are given 4 options, 3 are mentioned at the start and the fourth is to just end the program. 给它们提供了4个选项,开始时提到了3个,第四个是结束程序。

I managed to successfully get the first option to print out the employees without hassle, as is the same for the fourth option to end the program. 我成功地获得了第一个选择,可以轻松地打印出员工,就像结束程序的第四个选择一样。 I almost have the third one completed, I just need to find a way to print the name without a comma. 我几乎已经完成了第三个操作,我只需要找到一种不用逗号即可打印名称的方法。 My problem resides within the second option: how do I print the employees and their details if they fall between the minimum and maximum salary ranges entered by the user if the range isn't an integer since it has to include a '£' sign? 我的问题在于第二种选择:如果雇员及其详细信息不是用户输入的最小和最大工资范围之间的整数,并且该范围不是整数,则必须打印“£”,我该如何打印这些信息?

Here's the code. 这是代码。 It's the biggest chunk in the program because I just have no clue how to make it work properly - 这是程序中最大的块,因为我不知道如何使其正常工作-

def detailsPrint(field) :   #takes tuple and prints
print("{:30}" "{:6}" "{:15}" "{:7}".format(field[3] + ", " + field[4], field[0], field[2], "£" + field[1]))

if display == 2 :
        maxSalary = "£1000000"
        minpay = input("Enter the minimum pay : ")
        maxpay = input("Enter the maximum pay : ")
        if len(minpay) and len(maxpay) < maxSalary :
            for s in employeeList :
                if s[1] >= minpay :
                    detailsPrint(s)

The outcome should be something like (example) Simpson, Bart 12345 Consultant £55000 if the minpay were to be £50000 and maxpay £60000 结果应该是(例如) Simpson, Bart 12345 Consultant £55000Simpson, Bart 12345 Consultant £55000Simpson, Bart 12345 Consultant £55000如果最低工资是50000英镑,最高工资是60000英镑

edit: Managed to get it working. 编辑:设法使其工作。 Here's the code 这是代码

if display == 2 :
        x = False
        maxSalary = 1000000
        minpay = int(input("Enter the minimum pay: "))
        maxpay = int(input("Enter the maximum pay: "))
        if int(minpay) > int(maxSalary) or int(maxpay) > int(maxSalary) :
            x = False
            print("No employees earn over £1000000. Try again.")
        if int(minpay) or int(maxpay) < int(maxSalary) :
            for s in employeeList :
                if int(s[1]) >= minpay and int(s[1]) <= maxpay :
                    detailsPrint(s)
                    x = True
        if x == False :
            print("No employees could be found within that range. Try again")
            print("\n")

Simplest solution: don't ask for the £ char ;-) 最简单的解决方案:不要要求£char ;-)

A solution that work with your requirement is to change the line 符合您要求的解决方案是更改生产线

if len(minpay) or len(maxpay) > maxSalary :

with something like 用类似的东西

if int(minpay[1:]) > int(maxSalary[1:]) or int(maxpay[1:]) > int(maxSalary[1:]) :

which check the numeric value of the strings (your condition seems wrong anyway to me) 它检查字符串的数值(无论如何,您的条件对我来说似乎是错误的)

您可以在字符串中将所有“£”符号替换为“”。

YourString.replace("£", "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM