0

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.

1
  • 1
    What was wrong with the last two questions? Why didn't you just edit them? Commented Mar 26, 2011 at 4:24

2 Answers 2

2

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'
Sign up to request clarification or add additional context in comments.

Comments

1

This will work.

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

1 Comment

Also: somestring.isdigit() is True iff somestring consists entirely of digits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.