In: Computer Science
Task 1.
creating a nested if:
Need a python program that ask the user to enter the annual income of an employee and the years of experience. Pass these data to a function. The function decides if an employee does qualify for a loan or does not, then it prints a message based on the following rules:
If the income is equal or greater than $40000, the function checks if the employee's years of experience is greater than 4, if so the employee gets $6000 loan; otherwise, the amount of the loan will be $3500. However, if the income is less than $40000, the employee does not qualify for a loan.
Demo:
--define the function here ---
income,exp = input("\nEnter the income and years of experience: ").split()
theIncome = float(income)
theExp = float(exp)
--- validate the inputs before passing them ---
loan(theIncome, theExp)
Task2: Need a python program that pass two tuples to a function. The first one contains (devices codes); the second one contains the corresponding (IP addresses) of the devices. Both tuples are equal in size, and you can assign values for the tuples in the program. The function makes the first tuple elements, keys of a dictionary and the second tuple elements, values for the keys. The function then searches for a key in the formed dictionary to get its value (you can ask the user to enter the key). If the key is found, the function prints both the key and its value; otherwise a " key not found" message is printed.
Demo:
--define the function here ---
# the program starts here
tuple1 = ( , , , , , ) # fill with data
tuple2 = ( , , , , , ) # fill with data
makeDict (tuple1, tuple2)
Task 1 code -
income,exp=input("Enter the income and years of
experience:").split(" ")
theIncome = float(income)
theExp = float(exp)
def loan(emp_income,emp_exp):
if emp_income>=40000:
if emp_exp>4:
print("You qualify for a loan amount: $6000")
else:
print("You qualify for a loan amount: $3500")
else:
print("You dont qualify for a loan")
loan(theIncome, theExp)
Explanation -
1. We prompt user to enter income and experience with space between them. To extract income and experience from user's response , we use split function and pass space as parameter to the function i.e. spit(" ").This will split the input into 2 strings wherever it encounters space. if instead of space user entered response as 5000,4 then we will use comma in split function i.e. split(",") .
2.We then convert both income and experience intto float as input function reads them as strings.We store them in theIncome and theExp variable.
3.We define a function named loan which accepts emp_loan and emp_exp as parameters .if emp_income is greater than or equal to $40000, then function checks if emp_exp is greater than 4, if true then we print - You qualify for a loan amount: $6000
if emp_exp is not greater than 4 then we print - You qualify for a loan amount: $3500"
if emp_income is less than $40000 then we dont check emp_exp condition and print -You dont qualify for a loan
4.We finally call the function loan and pass theIncome and theExp as arguments to the function.
Task 2 code -
def makeDict(t1,t2):
new_dict={}
not_found_key=0
for i in range(len(t1)):
new_dict[t1[i]]=t2[i]
user_key=input("Enter a key: ")
for key,val in new_dict.items():
if user_key==key:
print("Your key is {} and its value is {}".format(key,val))
else:
not_found_key+=1
if not_found_key==len(t1):
print("key not found")
tuple1=('2135','2245','1234','4567')
tuple2=('192.168.1.1.','192.168.1.1.','255.255.255.0.','192.0.2.1.')
makeDict(tuple1,tuple2)
Explanation -
1. We define a function named makeDict which accepts tuple t1 and tuple t2 as parameters to the function.
2.We then declare an empty dictionary new_dict and variable not_found_key with value 0 which will be used later in the program.
3.We define a for loop which runs for iterations equal to size of t1 (you can even use t2 since both tuples have same size) using range function.We store every element in t1 as key to the dictionary new_dict and every element in t2 as value to the dictionary new_dict using syntax - dictionary[key]=value.
Here new_dict is the dictionary
t1[i] is the key where i ranges from 0 to size/length of t1 minus 1
t2[i] is the value where i ranges from 0 to size/length of t1 minus 1
4.We prompt user to enter a key and store it in variable user_key.
5.we then iterate through every key and value in dictionary new_dict and check if user_key is equal to key in dictionary new_dict. if yes , then we print both the key and value of the dictionary.
If no , then we increment not_found_key value by 1.
After completing the loop if not_found_key value is equal to size of t1 that means key was not found in every iteration .Hence we print key not found.
6.Finally we call the function makeDict and pass tuple1 and tuple 2 as arguments to the function makeDict