开发者

Join two for loops, separated by a print statement into one

In the following code, everything is working as expected.

It gets a 4 character long user input that ends with 0.

And simply adds stores in a dictonary the occurances of vowels and consonants.

input =""   #get input from user

while 4 < len(input) or 4 > len(input) or input[-1] != "0": #string has to be 4 char long and end in 0
开发者_如何转开发    input = raw_input("insert code:")

occurrences = {"a":0,"e":0,"i":0,"o":0,"u":0,"consonants":0}    #store the vouel count



for x in input:
    if x in occurrences.keys():
        occurrences[x] += 1  #count cowels
    elif x != "0":
        occurrences["consonants"] += 1   #count consonants


for x in occurrences:
    if occurrences[x] > 0 and x != "consonants":
        print x + ",",

print "were inserted",


for x in occurrences:
    if occurrences[x] > 0 and x != "consonants":
        print str(occurrences[x]) + ",",

print "times respectively"



if occurrences["consonants"] == 1:
    print "there was %d consonant"%occurrences["consonants"]
else:
    print "there was %d consonants"%occurrences["consonants"]

For the input "aef0" the program will print:

e, a, were inserted 1, 1, times

respectively there was 1 consonant

My questions is about this particular lines.

I know there must be a better way to do:

for x in ocurrances:
    if ocurrances[x] > 0 and x != "consonants":
        print x + ",",

print "were inserted",


for x in ocurrances:
    if ocurrances[x] > 0 and x != "consonants":
        print str(ocurrances[x]) + ",",

print "times respectively"

It just feels sloppy.

What I don't like about it is that I'm calling twice the same for loop and I feel this could be only one move in a much more elegant way, but I'm not finding the way to do so.

A pseudo code (or whatever) of what I'm trying to achieve would be the following.

loop the dictionary
print all key with values >= 1
print "were inserted" only once
print all the respective vales.
print "times respectively"

As I said I want the same output, but expressed in a more elegant way, I'm assuming the elegant would imply only one for loop but any other (more elegant) options are welcome!

I thought about doing something like this, but it's obviously not working. (Don't worry about it, it's just plain wrong, but the approach shows what I was aiming for)

Thanks in advance!


Another way to write your code might be something like this:

print ", ".join(k for k, v in occurrences.items() if k != "consonants" and v > 0),
print "were inserted"
print ", ".join(str(v) for k, v in occurrences.items() if k != "consonants" and v > 0),
print "times respectively"

You can shorten this a bit more by factoring out the search:

a = [(k, str(v)) for k, v in occurrences.items() if k != "consonants" and v > 0]
print ", ".join(x[0] for x in a), "were inserted",
print ", ".join(x[1] for x in a), "times respectively"


You have a few more problems with elegance and other things that matter more. For a start your users would revolt against having to type a 0 that you don't do anything meaningful with. In fact you need to dedicate code to ignoring it! Keeping the count of consonants in the dict is rather inelegant. You don't check whether the user typed all letters. You don't handle uppercase.

Here's some code that addresses those issues plus a few verbosities:

input = ""   # get input from user
while len(input) != 3 or not input.isalpha():
    input = raw_input("insert code:").lower()
ocurrances = {"a":0, "e":0, "i":0, "o":0, "u":0}
nconsonants = 0
for x in input:
    if x in ocurrances:
        ocurrances[x] += 1  #count vowels
    else:
        nconsonants += 1   #count consonants
for x in ocurrances:
    if ocurrances[x]:
        print x + ",",
print "were inserted",
for x in ocurrances:
    if ocurrances[x]:
        print str(ocurrances[x]) + ",",
print "times respectively"
if nconsonants == 1:
    print "there was 1 consonant"
else:
    print "there were %d consonants" % nconsonants

and you might like to change "ocurrances" to "occurrences". By the way, if the number of vowels is less than 2, the output is not very pretty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜