Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input data validation
#1
while True:
     try:
         x = int(input("Please enter a number: "))
         break
     except ValueError or x>10:
         print("Oops!  That was no valid number.  Try again...")
Copied from python.org.

I added in the "or x>10" as I need to keep the number below 10.

The data validation should be before the except ValueError step. How can I add the additional criterion?

Thanks.

P/S

Realised a backdoor to my problem.

while True:
     try:
         x = int(input("Please enter a number: "))
         if x < 10:
            break
     except ValueError or x>10:
         print("Oops!  That was no valid number.  Try again...")
Any better or proper way to solve this? Thanks.
Reply
#2
Add if block inside try
while True:
     try:
         x = int(input("Please enter a number: "))
         if 0 < x < 10:
              break
         else:
             print('Please, enter number between 0 and 10')
     except ValueError:
         print("Oops!  That was no valid number.  Try again...")
or inside else block

while True:
     try:
         x = int(input("Please enter a number: "))
     except ValueError:
         print("Oops!  That was no valid number.  Try again...")
     else:
         if 0 < x < 10:
              break
         else:
             print('Please, enter number between 0 and 10')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Aug-11-2021, 10:04 AM)buran Wrote: Add if block inside try
while True:
     try:
         x = int(input("Please enter a number: "))
         if 0 < x < 10:
              break
         else:
             print('Please, enter number between 0 and 10')
     except ValueError:
         print("Oops!  That was no valid number.  Try again...")
or inside else block

while True:
     try:
         x = int(input("Please enter a number: "))
     except ValueError:
         print("Oops!  That was no valid number.  Try again...")
     else:
         if 0 < x < 10:
              break
         else:
             print('Please, enter number between 0 and 10')
Noted and many thanks!

Cheerio.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 394 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  data validation with specific regular expression shaheen07 0 322 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  manually input data jpatierno 0 340 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 996 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,031 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Showing an empty chart, then input data via function kgall89 0 973 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 2,110 May-13-2022, 12:57 PM
Last Post: Cola_Reb
Question Input validation goes south. Gilush 6 2,675 Dec-04-2020, 12:18 PM
Last Post: Gilush
  data input chpyel 8 3,443 May-07-2020, 05:16 AM
Last Post: perfringo
  command line input (arg parse) and data exchange Simba 7 4,317 Dec-06-2019, 11:58 PM
Last Post: Simba

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020