Im new to coding and made this thing. How can i make it more efficient? I'd like to store it in a file and have some more information about every level but i can't get my head around that.
And if i want say 100 levels, what would be the best way then?
level = input("What is your level? ")
if level == '1':
salary = 5
elif level == '2':
salary = 5.5
elif level == '3':
salary = 6.05
elif level == '4':
salary = 6.65
elif level == '5':
salary = 7.32
elif level == '6':
salary = 8.05
elif level == '7':
salary = 8.85
elif level == '8':
salary = 9.74
elif level == '9':
salary = 10.71
elif level == '10':
salary = 11.78
print(salary)
CodePudding user response:
It looks like the salary corresponds to the second decimal floor value of the function salary = 5 * (1 0.1)**(level - 1)
.
I did this working example for you:
level = input('What is your level?')
try:
level = int(level)
if level < 1 or level > 10:
raise ValueError()
except ValueError:
raise ValueError('You must give a positive not null number <= 10.')
salary = 5 * 1.1**(level - 1)
floored_salary = int(salary * 100) / 100
print(floored_salary)
Results:
for level in range(1,11):
salary = 5 * 1.1**(level - 1)
print(f'{level=}, salary={int(salary * 100) / 100}')
---
level=1, salary=5.0
level=2, salary=5.5
level=3, salary=6.05
level=4, salary=6.65
level=5, salary=7.32
level=6, salary=8.05
level=7, salary=8.85
level=8, salary=9.74
level=9, salary=10.71
level=10, salary=11.78
CodePudding user response:
Use type dict :
level = int(input("What is your level? "))
Dict = {
1:5,
2:5.5,
3:6.05,
4:6.65,
5:7.32,
6:8.05,
7:8.85,
8:9.74,
9:10.71,
10:11.78,
}
salary=Dict[level]
print(salary)
CodePudding user response:
This syntax is also new to Python (3.10 onwards);
level = input("What is your level? ")
match level:
case '1':
salary = 5
case '2':
salary = 5.5
case '3':
salary = 6.05
case '4':
salary = 6.65
case '5':
salary = 7.32
case '6':
salary = 8.05
case '7':
salary = 8.85
case '8':
salary = 9.74
case '9':
salary = 10.71
case '10':
salary = 11.78
case _:
salary = 'undefined'
print(salary)
CodePudding user response:
Some workarounds to @ErnestBidouille great solution:
rate = list(map(lambda i: round(5 * (1 0.1)**(i - 1), 2), range(1, 101)))
level = int(input("What is your level? "))
salary = rate[level-1]
print(salary)
Or:
rate = list(map(lambda i: round(5 * (1 0.1)**(i - 1), 2), range(1, 101)))
while True:
try:
level = int(input("What is your level? "))
salary = rate[level-1]
break
except ValueError:
print('Numbers only')
except IndexError:
print('No level found')
print(salary)