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,...
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
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...
Write, save, and run a Python program Ask the user to enter a number of grams....
Write, save, and run a Python program Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.      4 marks for i in range(0,10): print(i) 2    what is wrong in the above python code? display your explanation using print statement. mark 3    insert the following comments into your program of part...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
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)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT