mardi 28 juin 2016

Is there a way to make this code less long but do the same thing?


sentence = input("What is your sentence?")
sentence=sentence.capitalize()
counta = sentence.count("a")
counte = sentence.count("e")
counti = sentence.count("i")
counto= sentence.count ("o")
countu= sentence.count ("u")
countA2 = sentence.count("A")
countE2 = sentence.count("E")
countI2 = sentence.count("I")
countO2 = sentence.count("O")
countU2 = sentence.count("U")
countI3 = sentence.count(" I ")
countspaces = sentence.count(" ")
a1 = sentence.count("!")
a2 = sentence.count(".")
a3 = sentence.count(">")
a4 = sentence.count("<")
a5 = sentence.count(":")
a6= sentence.count(";")
a7 = sentence.count("'")
a8 = sentence.count("@")
a9 = sentence.count("#")
a10 = sentence.count("~")
a11= sentence.count("{")
a12= sentence.count("}")
a13= sentence.count("[")
a14 = sentence.count("]")
a15 = sentence.count("-")
a16 = sentence.count("_")
a17 = sentence.count("+")
a18 = sentence.count("=")
a19 = sentence.count("£")
a20 = sentence.count("$")
a21= sentence.count("%")
a22 = sentence.count("^")
a23= sentence.count("&")
a24 = sentence.count("(")
a25= sentence.count(")")
a26=sentence.count("?")
count = (counta + counte + counti + counto + countu + countA2 + countE2 +     countI2 + countO2 + countU2 + countI3)
speci= a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15+a16+a17+a18+a19+a20+a21+a22+a23+a24+a25+a26)
print(sentence)
print("This has", speci, "special characters")
print("This has", countspaces, "Spaces")
print("This has", count, "vowels")

The above code as you can see is waaayyy too long. I dont know how to do the same thing (which is user inputs sentence and it counts vowels, spaces and special characters) but in less lines of code. If you know a better way. Please tell me. Because of course coding is all about the best code and this looks way to weird to be the best way. Thanks for your help


Aucun commentaire:

Enregistrer un commentaire