tags:

views:

115

answers:

2
m=input("Enter A Word (in lowercase): ")
i=0
length=0
for i in m:
    length=length+1

if "a">m>"z" :
    print("Input:" + m +" is incorrect! Please try again.")
    b=True
elif "A"<=m<="Z" or "A">m>"Z":
    b=True
    print("Input:" + m +" is incorrect! Please try again.")
elif "a"<=m<="z":
    b=False
    while b==False:
        for x in range(length):
            for y in range(x+1,length):
                if "a"<=m[x]<="z" and "a"<=m[y]<="z":
                    i=0
                    count=0
                    for i in range(length):
                        if m[i]=="a" or m[i]=="e" or m[i]=="i" or m[i]=="o" or m[i]=="u" :
                            count=count+1
                            consonants=length-count
                        else:
                            consonants=length-count                                                        
                else:

##                    print("Input:" + m +" is incorrect! Please try again.")
                    b=True
                    break
                break
        break



else:
    b=True
    print("Input:" + m +" is incorrect! Please try again.")


while b==True :
    m=input("Enter A Word (in lowercase): ")
    length=0

    for i in m:
        length=length+1

    if "a">m>"z" :
        print("Input:" + m +" is incorrect! Please try again.")
        b=True
    elif "A"<=m<="Z" or "A">m>"Z":
        b=True
        print("Input:" + m +" is incorrect! Please try again.")
    elif "a"<=m<="z":
        b=False
        while b==False:
            for x in range(length):
                for y in range(x+1,length):
                    if "a"<=m[x]<="z" and "a"<=m[y]<="z":


                        i=0
                        count=0
                        for i in range(length):
                            if m[i]=="a" or m[i]=="e" or m[i]=="i" or m[i]=="o" or m[i]=="u" :
                                count=count+1
                                consonants=length-count

                            else:
                                consonants=length-count    

                    else:

##                        print("Input:" + m +" is incorrect! Please try again.")
                        b=True
                        break

                    break
            break

    else:
        b=True
        print("Input:" + m +" is incorrect! Please try again.")

print("VOWELS   : " + str(count))
print("CONSONANTS : " + str(consonants))            
print(length)   

The problem is that when the user enters wrong input. then it has to print ("wrong input"), and ask for input again.

How can I stop the two for loops so that it only prints one time?

+2  A: 

I never thought one could write incomprehensible program in python.

At the end of it you just count vowels and consonants, if I were to remove the bells and whistles that is user input etc !!

Think differently :)

>>> vowels = 'aeiou'
>>> word = 'ddndnddjdnljdnddnwjddjwdbkbuydfadaddddnd'
>>> dict((c, word.count(c)) for c in word if c in vowels)
{'a': 2, 'u': 1}
>>> word = 'ddndnddjdnljdnddnwjddjwdbkbuydfadaddddndaaaaiiiee'
>>> dict((c, word.count(c)) for c in word if c in vowels)
{'a': 6, 'i': 3, 'u': 1, 'e': 2}
>>> 

Rework your program, see if you can simplify and edit your post. This is better than solving the issue of printing twice.

[Edit:]

Ok, You should write the program yourself. Some idea :

Idea:

While Flag is True
   word = get user input
   Another Flag = False
   for every character in word
       IF valid # if it is between a-z
          count vowels and consonants 
       ELSE 
          Another Flag = True
          break from loop
   IF Another Flag is True # then something went wrong

   ELSE
      # ALL went fine
      Print length, vowel count, consonant count
      Flag = False  
pyfunc
basically all i can say is there are lots of other ways but i have to make with it with just using if else and for loops
rahul
@rahul: Even then you can take a stab at changing your program. There are better ways. If you did that other issues might get resolved automatically as you will attain better clarity. By the way is this a homework?
pyfunc
no it is a program in my book and on monday i have midterem
rahul
This is a homework, and you should do it yourself. Stop asking for answers without even attempted to debug it. It's your education, not ours.
Kinderchocolate
A: 

To increase readability you might want to split your code up. Group different parts of functionality into functions. Perhaps you can make one function that deals with user input, one that validates the input and one that counts the vowels and consonants. With this approach you can concentrate on a single peace of functionality at a time.

Another benefit from this approach comes when you have to change functionality, when it is split up in logical blocks you don't have to dig through a lot of (spaghetti)code to find what you need to change and you reduce the risk of breaking a part of your program that already works correctly.

Jan