-2

Any mistake is there?

list1 = []
len = int(input('Enter the lenght you want : '))
for k in range(len):
    alp = input('Enter your word : ')
    list1.append(alp)
print()
print('The original List : ' , list1)

temp1 = 0
temp2 = 0

words = alp.split(" ")
for word in  words:
    if len(word) >= 5:
        temp1 += 1
    else :
        temp2 += 1

print(temp1)
print(temp2)
halfer
  • 19,824
  • 17
  • 99
  • 186
Ãlok
  • 3
  • 1

1 Answers1

-1

Here is the corrected version:

words = [] # try to use more descriptive variable names ('list1' is not clear, 'words' is better)

num_words = int(input('Enter the length you want : '))

for k in range(num_words):
    words.append(input('Enter your word : ')) # directly append the inputted word, no need for an intermediate variable

print()
print('The original List : ' , words)

result = 0

for word in words: # no need to split words, as it is already a list of words
    if len(word) >= 5:
        result += 1 # only count if it is longer than 5 characters

print(result)

As other commenters have mentioned, avoid renamed reserved names such as len, because this leads to mistakes.

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
  • I don't know sir that what is up vote or down vote ? – Ãlok Jun 10 '21 at 14:34
  • Small up arrow (or triangle) to the left of the answer. No worries though. – zr0gravity7 Jun 10 '21 at 14:44
  • Respected sir, I am writing a program to check the type of the variable like Digit,uppercase,lower case, special char. But I have functions like isupper(),is lower(),isdigit().But I am unable to find any function for a "Special char". How can I check a special char in the shortest way ? Kindly help me out with ? – Ãlok Jun 11 '21 at 11:49
  • @Ãlok see here: https://stackoverflow.com/questions/19970532/how-to-check-a-string-for-a-special-character – zr0gravity7 Jun 14 '21 at 17:23