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
USING PYTHON. Thank you in advance Write a program that allows the user to enter a...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a series of string values into a list. When the user enters the string ‘done’, stop prompting for values. Once the user is done entering strings, create a new list containing a palindrome by combining the original list with the content of the original list in a reversed order. Sample interaction: Enter string: My Enter string: name Enter string: is Enter string: Sue Enter string:...
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...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT