#๐ help with function no while loops allowed
20 messages ยท Page 1 of 1 (latest)
@vestal zodiac
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
def get_num(minimum:int) -> None:
x = int(input(f"enter an integer greater than or equal to {minimum}"))
if x >= minimum:
print(f"{x} is greater than or equal to {minimum}")
else:
print(f"{x} is not greater than or equal to {minimum}")
return get_num(minimum)
def print_name_age_v3() -> None:
agerange = ""
try:
name = str(input("please type your name" ))
age = int(input("please enter your age:" ))
return name, age
except ValueError or age < 0:
print("you are lying about your age")
return print_name_age_v3()
if age < 18:
agerange+= "child"
elif age in range(18,65,1):
agerange += "adult"
else:
agerange += "senior"
print(f"hello {name} {agerange}")
except can't take conditions
it would definitely be cool if it could, but it doesn't
except will only run when an exception is raised
okay
in fact, you could just re-use that function
i.e. ```py
print("enter your age")
age = get_num(1)
ohh oka
that function does have a slight bug that it doesn't return the age that gets entered, though
it would be useful if the function actually returned that age instead of just ignoring it
i.e. return x
so i should put the get_numback in my function like this?
def print_name_age_v3() -> None:
agerange = ""
name = str(input("please type your name" ))
age = int(input("please enter your age:" ))
age = get_num(age)
if ValueError or age < 0:
print("you are lying about your age")
return print_name_age_v3()
else:
if age < 18:
agerange+= "child"
elif age in range(18,65,1):
agerange += "adult"
else:
agerange += "senior"
print(f"hello {name} {agerange}")
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.