If an app asks for a user's age, it may be because the app requires a user to be over a certain age to use some of the services it provides. Write a function called checkAge that takes one parameter of type String. The function should try to convert this parameter into an Int value and then check if the user is over 18 years old. If he/she is old enough, print "Welcome!", otherwise print "Sorry, but you aren't old enough to use our app." If the String parameter cannot be converted into an Int value, print "Sorry, something went wrong. Can you please re-enter your age?" Call the function and pass in userInputAge below as the single parameter. Then call the function and pass in a string that can be converted to an integer.
Go back and update your function to return the age as an integer. Will your function always return a value? Make sure your return type accurately reflects this. Call the function and print the return value.
func checkage(age: String)->Int?
{
if let age_type = Int(age)
{
if age_type > 18 {
return "Welcome!"
}
else if age_type < 18 {
return"Sorry, but you aren't old enough to use our app."
}
}
else {
return "Sorry, something went wrong. Can you please re-enter your age?"
}
return age_type
}

Answers

Answer 1

Answer and Explanation:

Here the programming language swift is being used. There is a slight error in the program shown above:

var userInputAge=9

func checkage(age: String)->int?

{

if let age_type = Int(age)

{

if age_type > 18 {

return "Welcome!"

}

else if age_type < 18 {

return"Sorry, but you aren't old enough to use our app."

}

}

else {

return "Sorry, something went wrong. Can you please re-enter your age?"

}

return age_type

}

The program should be revised :

func checkage(age: int?)->String

{

if let age_type = Int(age)

{

if age_type > 18 {

return "Welcome!"

}

else if age_type < 18 {

return"Sorry, but you aren't old enough to use our app."

}

}

else {

return "Sorry, something went wrong. Can you please re-enter your age?"

}

return age_type

}

We call the functions :

checkage(userInputAge)

checkage("15")

Note: we revised the program for errors in the first line of the code where the int optional parameter(int?) was supposed to be used instead of the String parameter in the function. We then called the function using the userInputAge variable defined as the parameter and then we now also used a String as the parameter for calling the function the second time.

Answer 2

Answer:

def checkage(age: "String")->int:

   if age >= 18:

       return "Welcome!"

   return "Sorry, but you aren't old enough to use our app."

   

for _ in iter(list,0):

   myage = int(input("Please enter your age: "))

   if myage is int(myage):

       result = checkage(myage)

       print(result)

       break

   print("Sorry, something went wrong. Enter integer value as age.")

   

Explanation:

The python code above lets the user input the age value for continuous comparison. If the age is an integer, it checks to know if the age is greater than 18 or not. If yes, it returns "Welcome!" else "Sorry, but you aren't old enough to use our app". But if the age is not an integer, it displays the message "Sorry, something went wrong. Can you please re-enter your age?" then prompts the user again for the age.

Note: the ': "String" ' and "->int" is for documentation purposes. They are used to describe the type of parameters (for later) and return value (the former).


Related Questions

what are the advantages of using a folder?​

Answers

Answer:

1. easy access to files

2. better organization

Explanation:

Other Questions
Which of the following is not one of the views available in the PowerPoint application?Show ViewSlide SorterNotes PageReading Pane The coldest temperature last December was 18.24 degrees. The difference between this and the highest recorded temperature was 56.24 degrees. What was the highest recorded temperature? HOPEFULLY MY LAST ONE!!!!What is the slope?Simplify your answer and write it as a proper fraction, improper fraction, or integer. Recidivism occurs when a person continues to commit criminal behavior even after having been punished. A prison official wants to estimate the proportion of cases of recidivism. Examining the records of 250 convicts, the official determines that there are 65 cases of recidivism. Construct a 99% confidence interval for the proportion of cases of recidivism.A. (0.331, 0.567)B. (0.188, 0.332)D. (0.289, 0.431)E. (0.069, 0.111) PLS HELP ME ASAP!!!! In 2 hours, a canoeist can go 40 km downstream but only 6 km upstream. Find the rate of the current and the canoeist. You are walking along a stream when you look down and find a narrow bullet-like structure with an amberish color. You identify the fossil as the _________ of a(n) ____________. Inside the brown structure is a smaller striped What is the next number in this sequence? 3, 7, 11, 15.17211922 Whoever answers this first gets brainliestPls help ASAP Please help ASAP?? Gutuja Gutuja Normal or regular syntax follows which pattern? Answer choices: beginning, middle, end verb, subject, object subject, verb, object article, subject, verb When gold medalist Ren Qian prepared for her dive from the 10 meter platform during theOlympics, she looked out at her family sitting on the front row (level with the pool) at a 20angle of depression. How far away was Ren from her family (ground distance)? Round to thenearest tenth of a meter.Write answer like this: ##.# m.(5 Points)Enter your answer Name one field that was greatly influenced by the invention of perspective other than art.Group of answer choicesScienceArchitecturePhilosophyLiterature Which two fundamental forces are only attractive?O A. Gravitational and strong nuclearO B. Electromagnetic and weak nuclearO C. Electromagnetic and strong nuclearO D. Gravitational and weak nuclear Please answer 7. Shanaya graphed the line represented by theequation y= }X-6. Write an equation for aline that is parallel to the given line that goesthrough (2,10) Question 1 of 10What is one way banks can recover money they have loaned to someone whohas stopped making payments?O A. Banks can require the borrower's friends and family to pay off theloan.B. Banks can have collection agencies seize part of the borrower'sincome.C. Banks can force the borrower's employer to fire him or herimmediatelyD. Banks can make the borrower work at the bank for no salary. What is the absolute value of -8.35 Find the measure of each angle (help me please) someone helppp please!! Which of the following shows the correct orderof how sound travels through the ear?A) Pinna, tympanic membrane, ossicles,cochlea, cochlear nerveB) Auditory canal, ossicles, cochlea, vestibularnerveC) Pinna, tympanic membrane, vestibule, Carl makes the table below to determine the number of atoms of each element in the chemical formula 4Ca(ClO3)2.A table with 3 columns and 2 rows. The first column labeled element has entries calcium, chlorine, oxygen. The second column labeled calculation has entries 1 times 4 , 1 times 2, 3 times 2. The third column labeled total atoms has entries 4, 2, 6.What mistake did Carl make?