r/PythonProjects2 • • Aug 22 '22

Word counter(sorry for image quality)

Post image
37 Upvotes

10 comments sorted by

3

u/Loose-Sherbert8464 Aug 22 '22

Any tips on improving it?

17

u/notBruno3 Aug 22 '22

If you are trying to do an infinite loop with the "while b > a:", there is an easier way which is just to write "while True:"

3

u/Loose-Sherbert8464 Aug 22 '22

Ok thx

9

u/kaerfkeerg Aug 22 '22

Or

string = ""
while string.lower() != "q":
    # Your code

That way the user can quit the program by pressing "q" !

7

u/yolonir Aug 22 '22

Also split by default uses space delimiter, no need to specify it like that

0

u/lionlord_1 Aug 22 '22

How does count of words relate to count of spaces? Count of words = count of spaces + 1. So, you can find word_count using:

return string.count(‘ ‘)+1

16

u/Pieceofsh_t Aug 22 '22

why don't just While True

3

u/zebbodee Aug 22 '22

You can shorten the input and user instruction by combining them.

string=input("Enter your sentence here: ")

3

u/Pieceofsh_t Aug 22 '22

and string.split() is enough

0

u/[deleted] Aug 22 '22

[deleted]

1

u/Pieceofsh_t Aug 23 '22

split does it just right