1

My loop seems to be working Ok, until I leave the input empty. I want it just to loop to "enter new word or integer" but it is going through the loop and outputting the else statement 'multiple character types' also. If anyone could advise I would be grateful.

#hard code number
number=90


#whileloop
while True:
    enter_text = input("enter word or integer): ") 
        print()#loop if empty


     #check if all alpha
    if enter_text.isalpha():
        print(enter_text, "is all alphabetical characters! ")
        break


    #check<90>90   
    elif enter_text.isdigit():
        if int(enter_text) > number:
            print(enter_text, "is a large number")

        if  int(enter_text) <= number:
                print(enter_text,"Is smaller than expected")
        break


    #if conditions are not meet, multiple characters     
    else: 
        print(enter_text,'multiple character types')
1
  • yes, if input is all alphabetical, print output, stop code. if all digit assess against number print output, stop code, if input empty print("enter word or integer") then if bot alpha and digit, print "multiple character type" rerun code. hope this is clear. Commented Jun 14, 2017 at 9:44

2 Answers 2

1

You can do it like this:

#hard code number
number=90


#whileloop
while True:
    enter_text = raw_input("enter word or integer): ") 
    print()#loop if empty


     #check if all alpha
    if enter_text:
        if enter_text.isalpha():
            print(enter_text, "is all alphabetical characters! ")
            break


        #check<90>90   
        elif enter_text.isdigit():
            if int(enter_text) > number:
                print(enter_text, "is a large number")

            if  int(enter_text) <= number:
                    print(enter_text,"Is smaller than expected")
            break


        #if conditions are not meet, multiple characters     
        else: 
            print(enter_text,'multiple character types')
    else:
        print('You didn\'t write anything')
Sign up to request clarification or add additional context in comments.

Comments

1

Because enter_text is not isalpha() and not isdigit(), so this jumps into the else part. The behaviout is completly correct.

You should first of all check if it is None. You can do that e.g. like this:

if not enter_text: # will check if enter_text exists and is not a empty string e.g. ""
    continue
elif enter_text.isalpha():
    print(enter_text, "is all alphabetical characters! ")
    break
#check<90>90   
elif enter_text.isdigit():
    if int(enter_text) > number:
        print(enter_text, "is a large number")

    if  int(enter_text) <= number:
            print(enter_text,"Is smaller than expected")
    break
#if conditions are not meet, multiple characters     
else: 
    print(enter_text,'multiple character types') 

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.