Python: work with lists

Joined
Jan 26, 2023
Messages
9
Reaction score
0
Hello! Please give me a clue. There are 2 files in one name in another salary, while entering the name of a person, display his salary, that is, the number is in the same position as the name only from another file.
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
Hello! Please give me a clue. There are 2 files in one name in another salary, while entering the name of a person, display his salary, that is, the number is in the same position as the name only from another file.
  1. Read both the files: one with the names and one with the salaries
  2. Store the names and salaries in separate lists or dictionaries
  3. Get the input of the name from the user
  4. Use the input name to search for the corresponding salary in the lists or dictionaries
  5. Display the salary of the person
Here is a code example in Python using dictionaries:

Python:
# Read both the files
with open("names.txt") as name_file, open("salaries.txt") as salary_file:
    names = name_file.readlines()
    salaries = salary_file.readlines()

# Store the names and salaries in dictionaries
name_salary_dict = {}
for i in range(len(names)):
    name = names[i].strip()
    salary = salaries[i].strip()
    name_salary_dict[name] = salary

# Get the input of the name from the user
user_name = input("Enter the name: ")

# Display the salary of the person
if user_name in name_salary_dict:
    print("The salary of {} is {}".format(user_name, name_salary_dict[user_name]))
else:
    print("Name not found.")
 
Joined
Jan 26, 2023
Messages
9
Reaction score
0
Thank you a lot bro. I really value it. it seems to me it has not been a hard nut to crack for you. As long as don not you mind. Have a quick question. How much time had you spent before started be good at it ? I do it around the clock and such feel as just monkey business.
 
Joined
Dec 10, 2022
Messages
76
Reaction score
22
One more way

Python:
with open('Python/names.txt','r') as name_file, open('Python/salary.txt', 'r') as salary_file:
    names = map(str.rstrip, name_file)
    salary = map(str.rstrip, salary_file)
    data = dict(zip(names, salary))


user = input('>> ')
if user in data.keys():
    print(f'User: {user} Salary: {data[user]}')
else:
    print('User not found')
 
Joined
Jan 26, 2023
Messages
9
Reaction score
0
One more way

Python:
with open('Python/names.txt','r') as name_file, open('Python/salary.txt', 'r') as salary_file:
    names = map(str.rstrip, name_file)
    salary = map(str.rstrip, salary_file)
    data = dict(zip(names, salary))


user = input('>> ')
if user in data.keys():
    print(f'User: {user} Salary: {data[user]}')
else:
    print('User not found')
The better way have not found yet. Now going to be as busy as bee to put most part of it in def module
But thank you, I am in the red.

Python:
with open('eng_file.txt','r', encoding='utf-8-sig') as eng_file, open('rus_file.txt', 'r', encoding='utf-8-sig') as rus_file:
            eng = map(str.rstrip, eng_file)
            rus = map(str.rstrip, rus_file)
            data = dict(zip(eng, rus))
           
            n=input('what laguage? 1 Englis, 2 Russian')
            if int(n)==1:
                word =input('English ')
                if word in data.keys():
                    print(f'word: {word} is found: {data[word]}')
                if word not in data.keys():
                    print('word is not found')
                if word not in data.keys():
                    print('would you like to input the word to the dictionary?:1 yes,2 no')
                    c=input('')
                    if c=='1':
                           english,russian=input_word(english,russian)
                           print(english)
                           print(russian)
                    elif c=='2':
                        break
            if int(n)==2: 
                with open('eng_file.txt','r', encoding='utf-8-sig') as eng_file, open('rus_file.txt', 'r', encoding='utf-8-sig') as rus_file:
                    eng = map(str.rstrip, eng_file)
                    rus = map(str.rstrip, rus_file)
                    data = dict(zip(rus, eng)) 
                    word =input('Russian')
                    if word in data.keys():
                            print(f'word: {word} is found: {data[word]}')
                    if word not in data.keys():
                            print('word is not found')
                    if word not in data.keys():
                            print('would you like to input the word to the dictionary?:1 yes,2 no')
                            c=input('')
                            if c=='1':
                                   english,russian=input_word(english,russian)
                                   print(english)
                                   print(russian)
                            elif c=='2':
                                break
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,747
Members
45,545
Latest member
rapter____0

Latest Threads

Top