Home > OS >  Would like to know why my python code is not working
Would like to know why my python code is not working

Time:06-11

The purpose of this code is to allow a user to enter as many words as they would like, then add up the total number of words and divide to find the average length of the words.

My code is as follows:

num_of_words = 0
total_count = 0

user_input = input("Please enter a word.")

while user_input == '':
  length = len(user_input)
  total_count  = length
  num_of_words  = 1

  user_input = input("press enter to stop, or keep typing words.")

avglength = total_count/num_of_words
print(avglength)

The error code:

Traceback (most recent call last): File "C:/Users/USER/Documents/Python/Py_Ch5/word_counter.py", line 17, in avglength = total_count/num_of_words ZeroDivisionError: division by zero

THANK YOU!

CodePudding user response:

while user_input == '': is checking if user input is equal to an empty string. This is the opposite of what you want to be doing. By doing so, the while loop is skipped entirely and avglength evaluates to 0/0, resulting in the error you see.

Instead, simply check if user_input exists by doing:

while user_input:

Also, what if a user inputs no words? You need to account for that so that you do not divide by zero. Do so by checking before you divide:

if num_of_words > 0:
   avglength = total_count/num_of_words
else:
   avglength = 0

CodePudding user response:

You have two mistakes:

  • use != '' as loop condition as you're currently looping only if the user inputs nothing
  • check you have at least one word to compute the mean to avoid the ZeroDivisionError

Minimal update of your code for it to work:

num_of_words = 0
total_count = 0
user_input = input("Please enter a word.")
while user_input != '':
  length = len(user_input)
  total_count  = length
  num_of_words  = 1
  user_input = input("press enter to stop, or keep typing words.")
avglength = total_count/num_of_words if num_of_words else 0
print(avglength)

CodePudding user response:

It happens because when you enter some input that isn't just pressing Enter with no text, the value of user_input is not an empty string.

Therefore, your while loop body never runs, and num_of_words remains 0.

CodePudding user response:

Consider this pattern instead:

num_of_words = 0
total_count = 0

while True:
  user_input = input("Input a word, or press Enter key to stop")
  if user_input == '': break
  length = len(user_input)
  total_count  = length
  num_of_words  = 1
  
avglength = total_count/num_of_words
print(avglength)

We enter the loop, ask for input, test the input to see if we need to break, and if not then we run through the logic and loop once again.

CodePudding user response:

Rename variables for clarity.
Get rid of extra variables.
Place most of the code into an infinite loop, break on empty input.
Ask for the input only once.
Handle the case when no words are entered (use try).

num_words = 0
total_length = 0

while True:
    user_input = input("Please enter a word, press ENTER to stop: ")
    if user_input == "":
        break
    total_length  = len(user_input)
    num_words  = 1

try:
    avg_length = total_length / num_words
    print(avg_length)
except ZeroDivisionError:
    print("Empty input!")
  • Related