Python FOR loop Help, thank -


here's code class, i'm suppose use loop count number of times character used, told each character instead of line, mean? besides basic string line, have no clue of time, i'm online , can never understand teacher means of thetime. code creation have had trouble character counting, please help!

"""def main():          try:                  uppercase = 0                 lowercase = 0                 digits = 0                 whitespace = 0                  string_input = open('text.txt', 'r')                  character_strings = string_input.readlines()                  in range(len(character_strings)):                     character_strings[i] = character_strings[i].rstrip("\n")                  uppercase = uppercase_counter(character_strings)                 lowercase = lowercase_counter(character_strings)                 digits = digit_counter(character_strings)                  count1 = 0                 count2 = 0                 count3 = 0                  ch in string:                         if ch.isupper():                                 count1 = count1 + 1                  return count1                 print(count1)                  ch in string:                         if ch.islower():                                 count2 = count2 + 1                  return count2                 print(count2)                  ch in string:                         if ch.isdigit():                                 count3 = count3 + 1                      return count3                 print(count3)          except ioerror:                 print("the file not found")         except indexerror:                 print("there indexing error")         except:                 print("an error occurred")  main()  """ 

thank you! need help.

firstly, never use tabs in python. use 4-spaces indent, it's common practice. it's convenient when such simple things same way, helps understand else's code.

what telling you:

character_strings = string_input.readlines() 

this line reads text string_input file, splits newline symbol , returns list of lines. can see, there no need care lines, can read whole text single long string

string = string_input.read() 

so should pass string _counters have.

uppercase = uppercase_counter(string) 

and on.

about doing same loops, need iterate on symbols of string

for symbol in string:     if symbol.isupper():         count1 = count1 + 1 

same islower , isdigit

(naming variables something1, something2 , something3 bad practice. perhaps should think of better names.)

p. s. forgot close file. insert string_input.close() before first except. it's recommended close file when done it.


Comments