Write an interactive script that asks for the user's age. If it is equal to or higher than 21, print a message saying that this user is allowed to drink alcohol.If the user's age is below 21, print a message telling the user how many years he or she has to wait before legally being allowed to drink.Display your script on the terminal using the cat command. Run the script and give inputs 15, then 30.

Respuesta :

Answer:

Hi Lizzie! Please find the answer with explanation below.

Explanation:

We can implement this question in many languages, and since the language is not indicated in the question, I have chosen Python 2.0 + to implement it in (for it to work in Python 3.0+ you will need to change the "raw_input" to "input". You can save the below code in a file called drinking_age.py.

cat drinking_age.py

age = raw_input("Please enter your age: ")

try:

 age = int(age)

 if age >= 21:

   print("This user is allowed to drink alcohol")

 elif age < 1:

   print("Age cannot be less than 1")

 else:

   print("You have to wait " + str(21 - age) + " before legally being allowed to drink")

except ValueError:

 print("Invalid input!")