By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,274 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
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
  1. def ck_no(prompt):
  2.     while True:
  3.         try:
  4.  
  5.             lst = (2,3,4)
  6.             if prompt not in lst:
  7.                 print("not in list")
  8.                 raise ValueError
  9.             else:
  10.                 print("In the List")
  11.         except ValueError as e:
  12.             print(e)
  13.             return
  14.  
  15. ck_no(int(input(print("Enter a No: "))))
  16.  
Expand|Select|Wrap|Line Numbers
  1. Enter a No: 
  2. 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
Share this Question
Share on Google+
5 Replies


P: 94
Loop must be in main routine.
Expand|Select|Wrap|Line Numbers
  1. def ck_no(prompt):
  2.  
  3.     lst = (2,3,4)
  4.     if prompt in lst:
  5.         print("In the List")
  6.         return False
  7.     else:
  8.         print('not in list')
  9.         return True
  10.  
  11. flg = True
  12. while flg:
  13.     flg = ck_no(int(input('Enter a No: ')))
  14.  
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
  1. def ck_no():
  2.     """
  3.     Ask for user input of a valid number and repeat the
  4.     question, if the user enters a number, which is not
  5.     allowed or an invalid number.
  6.  
  7.     Allowed numbers: 2, 3, 4
  8.  
  9.     Return value of the valid answer as int.
  10.     """
  11.     valid_choice = (2, 3, 4)  # Pep8, always a white space after the comma
  12.     # better names helps other people to understand
  13.     # the code
  14.     while True:
  15.         answer = input("Enter a No: ")
  16.         # get the answer
  17.         # we need this for later output if
  18.         # there was an exception while casting to an int
  19.         try:
  20.             value = int(answer)
  21.             # here a ValueError occurs if answer is an integer  
  22.         except ValueError:
  23.             print("Invalid input:", answer)
  24.             # catch this exception
  25.             # print the error
  26.             # don't do further checks, continue on the top
  27.             # of the loop
  28.             # so, the rest is skipped
  29.             continue
  30.         # this point is reached, if no ValueError happens
  31.         if value in valid_choice:
  32.             # simple check if a value is in a sqeuence or collection
  33.             # return the value, if the value was in the list
  34.             # the value is an int
  35.             return value
  36.         # if value was not in valid_choice, the if-block is not executed
  37.         # no return, so this line of code is reached
  38.         print("Value", value, "is not a valid choice")
  39.         # just print that the value was not in the tuple
  40.  
  41. 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
  1. def ck_no(prompt):
  2.     lst = ("2","3","4")
  3.     if prompt in lst:
  4.         print('In the List')
  5.         return False
  6.     else:
  7.         print('not in list')
  8.         return True
  9.  
  10. flg = True
  11. while flg:
  12.     flg = ck_no(input('Enter a No: '))
1 Week Ago #6

Post your reply

Sign in to post your reply or Sign up for a free account.