Question

In: Computer Science

In Python, write a program that allows the user to enter a number between 1-5 and...

In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should:

  • Continue to run until the user quits and
  • Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5),
  • If the user enters zero “0” the program should terminate

You can use the following facts:

1- Vitamin A protects eyes from night blindness and age-related decline

2- Vitamin B6 promotes brain health and reduces Alzheimer's risk

3- Vitamin B12 may improve health by decreasing homo-cysteine

4- Vitamin C protects your health from cardiovascular issues, cancer and strokes

5- Vitamin D reduces the risk of diabetes

All code must be contained in a function ( except calling main() )

printWelcome(): function that prints welcome message    

getValidInput(): takes user input until valid and returns the valid input to main()

main() – allows user to continue enter numbers until entering zero “0” to quit, uses getValidInput(): to verify the input, then prints the vitamins benefits

Solutions

Expert Solution

Python code:

#defining printWelcome function
def printWelcome():
#printing Welcome message
print("Welcome! know different vitamins here.")
#defining getValidInput function
def getValidInput():
#accepting number from user
n=input("Enter a number between 1 to 5: ")
#initializing a valid numbers list
numbers=['0','1','2','3','4','5']
#checking if the number is not valid
while(n not in numbers):
#printing Invalid input
print("Invalid input:")
#accepting number from user
n=input("Enter a number between 1 to 5: ")
return n
def main():
#printing Welcome message
printWelcome()
#infinite loop till user enter 0
while(True):
#calling getValidInput and getting input
num=getValidInput()
#checking if the number is 1
if(num=='1'):
#printing its message
print("Vitamin A protects eyes from night blindness and age-related decline")
#checking if the number is 2
elif(num=='2'):
#printing its message
print("Vitamin B6 promotes brain health and reduces Alzheimer's risk")
#checking if the number is 3
elif(num=='3'):
#printing its message
print("Vitamin B12 may improve health by decreasing homo-cysteine")
#checking if the number is 4
elif(num=='4'):
#printing its message
print("Vitamin C protects your health from cardiovascular issues, cancer and strokes")
#checking if the number is 5
elif(num=='5'):
#printing its message
print("Vitamin D reduces the risk of diabetes")
else:
#exiting loop
break
if __name__ == "__main__":
main()


Screenshot:


Input and Output:


Related Solutions

In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number...
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number of data set. 2- Allows the user to exit the program at any time by entering zero. 3- Calculates and displays the following statistics for the entered data set: a- Count of positive, negative, and total numbers. b- Maximum and Minimum numbers. c- Sum and Average of positive numbers. d- Sum and Average of negative numbers. e- Overall Sum and overall average of all...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663. This is my code, but I cannot figure out where to go from here. #set new number new_number = "" #split number split_num = phone.split("-") for char in split_num[1:2]:...
Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
In Python write a program that asks the user to enter the monthly costs for the...
In Python write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance the program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. your program MUST have BOTH a main function AND a function named calcExpenses to calculate the expenses. DO NOT display the expenses inside of the calcExpenses function!!...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT