In: Computer Science
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:
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
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: