如何结合str和int变量?

时间:2018-10-10 02:27:56

标签: python

对于这个问题,我需要“分离”或标识3种不同用户输入的单独集合:姓名,地址和薪水。然后,该程序需要找到最高薪水并打印其所属人员的姓名和地址。

我不确定如何在Python中做到这一点。

我知道我可以使用max(salary),但如何打印相关的名称和地址?

我正在尝试使用while循环来做到这一点。 编辑: 好的,所以我是个初学者,如果这太基础了,我深表歉意。 到目前为止,这是我想出的。

function interpolateParams<T extends {[P in keyof T] : string | number}>(
    route: string, 
    params: T) : string { /*...*/ }

谢谢

2 个答案:

答案 0 :(得分:0)

max_salary = None
max_index = None
salary = 1
index = 0
x = []
while salary > 0:
    name = raw_input("Input name: ")
    address = raw_input("Input address: ")
    salary = int(raw_input("Input salary or a number < 0 to end program: "))
    if max_salary is None or salary > max_salary:
        max_salary = salary
        max_index = index
    index += 1
    x.append("{}  {}  {}".format(name, address, salary))

if max_index  is not None:
    print(x[max_index])

另一种方式(首选):

x = []
while True:
    name = raw_input("Input name: ")
    address = raw_input("Input address: ")
    salary = int(raw_input("Input salary or a number < 0 to end program: "))
    if salary < 0:
        break
    x.append((name, address, salary))

if x:
    print("{}  {}  {}".format(*max(x, key=lambda t: t[2])))

答案 1 :(得分:0)

这应该工作正常。

people=[]

elements= int(input('enter the number of people you want: '))

 for i in range(elements):
     name= input('enter the name of person %d: '% (i+1))
     address= input('enter the address: ')
     salary= int(input('enter the salary: '))
     person={}
     person['name']=name
     person['address']= address
     person['salary']= salary
     people.append(person)

# getting the max from all the salaries and the max
sal_max= max([x['salary'] for x in people])


 # matching the max salary:
for i in people:
    if i['salary']==sal_max:
    print (i['name'], i['address'])