Home > Mobile >  How to extract values from a loop that stop at a specified n
How to extract values from a loop that stop at a specified n

Time:11-04

I am trying to create tuples from 2 input. For example, my program will ask how many people is there. Let's say I write 3. The program will ask profession of each one and his salary. After that, I want the program to display a tuple containing (profession, salary) is ascending order of salary. What im trying to achieve:

#1-Number of person = Input:(Please write the number of person you want to analyze) (Lets say 3)

#2-Profession1:Doctor Salary1:350000 Profession2:Teacher Salary 2:60000 Profession3:CEO Salary3:1000000

#3-Then I want my programm to display this in the console : ('Teacher',60000) ('Doctor',350000) ('CEO',1000000)

number_person=input("Write the number of person: ")

for i in range(int(number_person)):
     profession=input("Write profession")
     salary=input("Write salary")

CodePudding user response:

make a list of strings

number_person=input("Write the number of person: ")
str_list =[]

for i in range(int(number_person)):
    profession=input("Write profession")
    salary=input("Write salary")
    str_list.append(f"({profession},',',{salary})")
         
for i in str_list:
    print(i)

CodePudding user response:

You need to save the pair in a main list, then print the list

values = []
number_person = int(input("Write the number of person: "))
for i in range(number_person):
    profession = input("Write profession: ")
    salary = int(input("Write salary: "))
    values.append((profession, salary))

for item in sorted(values, key=lambda x: x[1]):
    print(item)
  • Related