'python 3 - beginner question if/else Q's with errors
Python 3 (not 2)
The question: A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net bonus amount.
There is an answer on the website but when I run the given answer there is an error..also tried all kinds of "corrected" versions but each time there is a different error. I guess there is a couple of errors..
print ("enter salary")
salary = input()
print ("enter year of service")
yos = input()
if yos>5:
print ("bonus is"),0.05*salary
else:
print ("no bonus")
please help with the corrected code (: thank you
Solution 1:[1]
yos and salary are taken as a str, where as 5 is an integer, also you cannot multiply 0.05 with str(which is salary here) I have given the corrected code below
salary=int(input("enter salary"))
yos=int(input("enter year of service you have given to the company"))
if yos>5:
print ("bonus is",0.05*salary)
else:
print ("no bonus")
Solution 2:[2]
a=int(input("Enter salary"))
b=int(input("Enter year of service"))
c=a+a/20 #a/20 gives 5%
if(b>5):
print(c)
else:
print(a)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chinmay Singhal |
| Solution 2 | Jonas Eberle |
