How do I create a function/method out of this code and then call it to the main()?
Question:
enter code here
This is the original code that I wrote:
while True:
user_input = (input(">>",))
try:
user_input = int(user_input)
except ValueError:
pass
if user_input in range(1, len(something):
break
I want to put in a method:
`get_user_answer(n: int, p: str) -> int` # This is what the method should look like kind of
#But what should I write here?
def main()
# and how would I call it?
main()
I’m learning about methods so I’m confused
I’m expecting it to work like the code I first wrote, but in a method instead that I call to the main function.
Answers:
# get user answer function
def get_user_answer():
while True:
user_input = (input(">>",))
try:
user_input = int(user_input)
except ValueError:
pass
if user_input in range(1, len(something):
break
# main function
def main():
# calling get_user_answer function here
get_user_answer()
#calling main function here
main()
To declare a function in python use def .
def get_user_answer(n: int):
input_number = 0
while not int(input_number) in range(1,n):
shift = input(">>")
return input_number
# You dont 'need' to declare a main function for smal programms. But here is a example.
def main()
valid_user_number = get_user_answer(26)
main()
Thanks everyone for trying to help, I managed to figure it out. If anyone is interested it was:
def get_user_answer(max_num: int, prompt: str) -> int:
while True:
user_input = input(prompt)
try:
user_input = int(user_input)
except ValueError:
pass
if user_input in range(1, max_num):
break
print(f"Write a number between - {max_num}")
return user_input
I probably didn’t give enough information to help me in the best way, now that I think about it, but thanks anyway.
and calling the function to main:
user_input = get_user_answer(len(something), ">>")
enter code here
This is the original code that I wrote:
while True:
user_input = (input(">>",))
try:
user_input = int(user_input)
except ValueError:
pass
if user_input in range(1, len(something):
break
I want to put in a method:
`get_user_answer(n: int, p: str) -> int` # This is what the method should look like kind of
#But what should I write here?
def main()
# and how would I call it?
main()
I’m learning about methods so I’m confused
I’m expecting it to work like the code I first wrote, but in a method instead that I call to the main function.
# get user answer function
def get_user_answer():
while True:
user_input = (input(">>",))
try:
user_input = int(user_input)
except ValueError:
pass
if user_input in range(1, len(something):
break
# main function
def main():
# calling get_user_answer function here
get_user_answer()
#calling main function here
main()
To declare a function in python use def .
def get_user_answer(n: int):
input_number = 0
while not int(input_number) in range(1,n):
shift = input(">>")
return input_number
# You dont 'need' to declare a main function for smal programms. But here is a example.
def main()
valid_user_number = get_user_answer(26)
main()
Thanks everyone for trying to help, I managed to figure it out. If anyone is interested it was:
def get_user_answer(max_num: int, prompt: str) -> int:
while True:
user_input = input(prompt)
try:
user_input = int(user_input)
except ValueError:
pass
if user_input in range(1, max_num):
break
print(f"Write a number between - {max_num}")
return user_input
I probably didn’t give enough information to help me in the best way, now that I think about it, but thanks anyway.
and calling the function to main:
user_input = get_user_answer(len(something), ">>")