Question

In: Computer Science

Task 1. creating a nested if:   Need a python program that ask the user to enter...

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)

Solutions

Expert Solution

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


Related Solutions

In your python program, ask the user to enter the annual income of an employee and...
In your python program, 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,...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the user to enter the student names and scores as shown. Output the name and score of the student with the 2nd lowest score. NOTE: You may assume all students have distinct scores between 0 and 100 inclusive and there are at least 2 students NOTE: You may only use what has been taught in class so far for this assignment. You are not allowed...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
1. Write Python code to - (a) ask user to enter a list of animals, one...
1. Write Python code to - (a) ask user to enter a list of animals, one item at a time, until “done” to terminate input (b) randomly display a selected item from the list, starting with “I like ______”. When run, the output should look something like this: Enter an item; 'done' when finished: cats Enter an item; 'done' when finished: dogs Enter an item; 'done' when finished: guinea pigs Enter an item; 'done' when finished: done You are done...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT