+ 2
Intermediate Python - 13.2 Practice - Getting a Raise - help
Problem: You work on a payroll program. Given a list of salaries, you need to take the bonus everybody is getting as input and increase all the salaries by that amount. Output the resulting list. Current attempt, have tried several iterations: salaries = [2000, 1800, 3100, 4400, 1500] bonus = int(input()) result = list(map(bonus, salaries)) print(result) Not sure what to do? Please help, thanks!
11 Answers
+ 13
Zach Z ,
the use of map() function needs a function that will be applied to each element in the salaries list. this can be a lambda function as mentioned from I ᴀᴍ "Tɪᴍᴇ" .
but it can also be a regular function, if you feel more comfortable with that: (this is your code slightly adjusted)
def add(salary):
return salary + bonus
salaries = [2000, 1800, 3100, 4400, 1500]
bonus = int(input())
result = list(map(add, salaries))
print(result)
but this task can also be solved by using a loop.
+ 5
Zach Z
Use lambda:
list(map (lambda x : x + bonus, salaries))
+ 3
Map() Function:
salaries = [2000, 1800, 3100, 4400, 1500]
bonus = int(input())
def add_bonus(x):
return x + bonus
result = list(map(add_bonus, salaries))
print(result)
+ 2
Thanks I ᴀᴍ "Tɪᴍᴇ" and Lothar !
+ 1
def add(amount):
return amount + bonus
salaries = [2000, 1800, 3100, 4400, 1500]
bonus = int(input())
toatal = list(map(add, salaries))
print(toatal)
+ 1
With For loop
salaries = [2000, 1800, 3100, 4400, 1500]
bonus = int(input())
listbonus=[]
for i in salaries:
listbonus.append(i+bonus)
print(listbonus)
+ 1
My Answer:
def add(salary):
return salary + bonus
salaries = [2000, 1800, 3100, 4400, 1500]
bonus = int(input())
result = list(map(add, salaries))
print(result)
0
salaries = [2000, 1800, 3100, 4400, 1500]
bonuses = int(input())
result = list(map(lambda bonus: bonus + bonuses, salaries))
print(result)
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int salaryBudget = Convert.ToInt32(Console.ReadLine());
int percent = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Before the increase: " + salaryBudget);
//complete the method call
Increase(ref salaryBudget, percent);
Console.WriteLine("After the increase: " + salaryBudget);
}
//complete the method
static void Increase(ref int x, int y)
{
x = (x * (100 + y )) / 100;
}
}
}
This code works 100 percent without error.
0
salaries = [2000, 1800, 3100, 4400, 1500]
bonus = int(input())
salaries = list(map(lambda x: x + bonus, salaries))
print(salaries)
0
Wow it's good thing I like it