False
?
False
To ensure our condition (number < 50
) will eventually be evaluated as False
, we need to updated number
inside our loop:
Go to Python Tutor to visualize how the while
loop runs.
Factorial: 5! = 1 * 2 * 3. * 4 * 5 = 120
Write a function called add_up_to
that takes an numeric argument n
. The function should add all numbers from 1 to n
in a while loop, and then (outside the loop) return the sum
Name your file sum_up.py
(your filename is different than your function name) and submit it to attendance on gradescope
Modify the code below to use a while
loop to request a valid input from the user.
def age_milestones(age):
'''
This function prints an informative message based on,
a person's age.
Args:
age: integer representing a person's age
Returns:
A string with a message to the user
'''
message = ""
if age >= 18:
message += 'You may apply to join the military'
if age >= 21:
message += 'You may drink'
if age > 35:
message += 'You may run for president'
return message
def validate_age(age):
return age.isnumeric()
def main():
'''
This functions takes input from the user and calls the
check_age() functiont to print a message
'''
age = input('How old are you?\n')
if validate_age(age):
age = int(age)
print(age_milestones(age))
else:
print("Invalid age entered")
main()
Modify the code below to use a while
loop to request a valid input from the user.
def age_milestones(age):
'''
This function prints an informative message based on,
a person's age.
Args:
age: integer representing a person's age
Returns:
A string with a message to the user
'''
message = ""
if age >= 18:
message += 'You may apply to join the military'
if age >= 21:
message += 'You may drink'
if age > 35:
message += 'You may run for president'
return message
def validate_age(age):
if age.isnumeric() and int(age) < 150:
return True
return False
def main():
'''
This functions takes input from the user and calls the
check_age() functiont to print a message
'''
age = input('How old are you?\n')
while validate_age(age) == False:
print("Invalid age entered. Please enter a valid age.")
age = input('How old are you?\n')
age = int(age)
print(age_milestones(age))
main()