446,274 Members | 1,823 Online
Need help? Post your question and get tips & solutions from a community of 446,274 IT Pros & Developers. It's quick & easy.

# How to make the function to prompt the user repeatedly till he enters a correct no wh

 P: 3 Expand|Select|Wrap|Line Numbers def ck_no(prompt):     while True:         try:               lst = (2,3,4)             if prompt not in lst:                 print("not in list")                 raise ValueError             else:                 print("In the List")         except ValueError as e:             print(e)             return   ck_no(int(input(print("Enter a No: "))))   Expand|Select|Wrap|Line Numbers Enter a No:  None After calling the function it prompts the user to enter the no. But it prints none. Beside none if enter the no it accepts and the pgm continues. How to correct this none. I want to prompt the user again and again to enter different nos. How to do this? 1 Week Ago #1
5 Replies

 P: 94 Loop must be in main routine. Expand|Select|Wrap|Line Numbers def ck_no(prompt):       lst = (2,3,4)     if prompt in lst:         print("In the List")         return False     else:         print('not in list')         return True   flg = True while flg:     flg = ck_no(int(input('Enter a No: ')))   1 Week Ago #2

 P: 3 Hi SioSio, Thanks, I ran this code in Thonny. It work's. Can u please explain how this return statement is connected to the flag. What ever is coming from return it is checking with the flag. How does this return work? 1 Week Ago #3

 P: 3 Hi SioSio, I was given the below code by another kind person like u. What is ur comment? Both works fine. Thanks. Expand|Select|Wrap|Line Numbers def ck_no():     """     Ask for user input of a valid number and repeat the     question, if the user enters a number, which is not     allowed or an invalid number.       Allowed numbers: 2, 3, 4       Return value of the valid answer as int.     """     valid_choice = (2, 3, 4)  # Pep8, always a white space after the comma     # better names helps other people to understand     # the code     while True:         answer = input("Enter a No: ")         # get the answer         # we need this for later output if         # there was an exception while casting to an int         try:             value = int(answer)             # here a ValueError occurs if answer is an integer           except ValueError:             print("Invalid input:", answer)             # catch this exception             # print the error             # don't do further checks, continue on the top             # of the loop             # so, the rest is skipped             continue         # this point is reached, if no ValueError happens         if value in valid_choice:             # simple check if a value is in a sqeuence or collection             # return the value, if the value was in the list             # the value is an int             return value         # if value was not in valid_choice, the if-block is not executed         # no return, so this line of code is reached         print("Value", value, "is not a valid choice")         # just print that the value was not in the tuple   ck_no() 1 Week Ago #4

 P: 94 Function ck_no was changed to boolean function. Returns “True” if the input data is not in lst, so it prompts for input again not exit the main routine loop. If the input data is in lst, it returns “False”, so it exits the loop and ends processing. 1 Week Ago #5

 P: 94 I thought about this way by moving the input processing to the function ck_no, too. But in the original program, the input processing was in the main routine, so I showed what I posted. In the code posted before, when the input data is not numeric value, it stops with an error, so it was modified. Expand|Select|Wrap|Line Numbers def ck_no(prompt):     lst = ("2","3","4")     if prompt in lst:         print('In the List')         return False     else:         print('not in list')         return True   flg = True while flg:     flg = ck_no(input('Enter a No: ')) 1 Week Ago #6