The Woo
The Woo

Reputation: 18675

Python - Check Multiple String Value For Chars

I know this is a double up of the other questions I have asked today. But this is the most current, and I have put in for the others to be deleted.

This is my code currently:

def Main():

chars = set('0123456789')

while True:

   Class_A_Input = raw_input('Enter Class A tickets sold: ')

   Class_B_Input = raw_input('Enter Class B tickets sold: ')

   Class_C_Input = raw_input('Enter Class C tickets sold: ')

   if any((c in chars) for c in Class_A_Input):

       break

   else:

       print 'Wrong'

I have been able to use the 'if any((c in chars) for c in Class_A_Input)' when there is only 1 user input at a time.

Is there a way to check with this kind of method, all 3 user inputs, and to break the loop if they are fine, otherwise to display 'Wrong' and start the loop over again for the user to input.

Thanks for your patience and help.

Upvotes: 0

Views: 480

Answers (2)

wong2
wong2

Reputation: 35750

I think you want to check if the input of users has digit number, if that is right, then you can use

import re

def hasDigit(s):
    return not not re.search("\d", s)

def Main():
    while True:
        Class_A_Input = raw_input('Enter Class A tickets sold: ')
        Class_B_Input = raw_input('Enter Class B tickets sold: ')
        Class_C_Input = raw_input('Enter Class C tickets sold: ')

        if all([hasDigit(Input) for Input in [Class_A_Input, Class_B_Input, Class_C_Input]]):
            break
        else:
            print 'Wrong'

Upvotes: 2

Rumple Stiltskin
Rumple Stiltskin

Reputation: 10415

This will work.

try:
   int(Class_A_Input)
   int(Class_B_Input)
   int(Class_C_Input)
   break
except ValueError:
   print "Wrong"

Upvotes: 1

Related Questions