1

I'd like to test an inputted string against certain conditions. In this instance, I want the string from the variable to contain at least one numeric character...

while any(inputted.isdigit()) == False:
    print ("Inputted must contain at least one numeric character") # can't figure this one out
    inputted = input("Please enter a combination: ")

An ideal input would be a string like "Boat88Sea"

Any help would be greatly appreciated!

2
  • Can you please give an example of what kind of inputs you are giving? What is a typical example of what is in inputted Commented Apr 4, 2016 at 3:04
  • An ideal input would be a string like "Boat88Sea" Commented Apr 4, 2016 at 3:07

2 Answers 2

4

You must iterate over the input string:

input_string = "" 
while any(c.isdigit() for c in input_string) == False:
    print ("the input string must contain at least one numerical character") 
    input_string = input("Please enter a combination: ")

The moment any character in the string is found to be a digit, the while loop will exit.

Alternatively, this might read even closer than what you want:
As long as not any character is a digit in the input string...

while not any(c.isdigit() for c in inputted):
Sign up to request clarification or add additional context in comments.

2 Comments

you don't need the square brackets, you can just use a generator instead of a list.
Correct, thank you; I changed it - but I doubt it makes much of a difference, it is unlikely the input will be large.
1

There are definitely better way than this :

cond = True
while cond:
    print ("Inputted must contain at least one numeric character") 
    inputted = input("Please enter a combination: ")
    for x in range(0,9) : 
        if str(x) in inputted : cond = False

Comments

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.