Python Forum

Full Version: question about simple algorithm to my problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
question about this line

#!/usr/bin/python3

names = [] # list
outF = open("myOutFile.txt", "w")

total = 0
p = '\n'
char_count = "Char count: "
lines_of_text = 1

def findLength (str): # total characters of word
    counter = 0
    counter_total = 0
    for p in str:
        counter += 1 # each word
    return counter

def strTotal (str): # total characters in 3 words
    counter = 0
    counter_total = 0
    for p in str: 
        counter = findLength(str)
        counter_total += counter
    return counter_total

for number in range (1, 4):
    scan_input = input ("Enter line: ")
    names.append(f"name: {scan_input}")
    outF.write (p)
    outF.write(scan_input)
    outF.write(p)
    outF.write ("Char count: ")
    lines_of_text += 1
    print (" ", scan_input, findLength (scan_input))

print ("How many lines of text: ", lines_of_text - 1)
print ("How many characters of text (total chars): ", strTotal (scan_input)) # total characters
Also, i was wondreing, would it be better to write

def strTotal (x)
with a int value (x), and somehow get the variable from findLength function (i googled counter = findLength() could work). And also, is this where my Data Structure and Algorithm learning comes into play, inside the for loop inside the strTotal function (writing statements with the compound assignment operator (writing an algorithm). thanks, please would just like to print out the final print statement for a total of characters for the three words (character's added up out of all three words. The output to text file works.
print ("How many characters of text (total chars): ", strTotal (scan_input)) # total characters
Smile
Your strTotal() function doesn't work because you are only passing the last word entered.

Why aren't you using the built-in Python function len()?

You should always close any open files before exiting the program.

Why are you counting characters twice? You count them for each word, then you count all the words a second time to get the total.

Do not use str as an argument name. str is a special Python word. Using it as an argument name prevents your function from using the str() function. It also causes a lot of confusion because you can use type annotation in Python, and str appearing in an argument list usually indicates the argument type. For example, your findLength function could be written like this:
def findLength(word:str) -> int:
    '''Return length of word'''
    return len(word)
The annotation says that this function expects one argument that is a str and returns an int value. A good IDE will use this information to provide hints to help you write code that uses this function. Code analysis tools can use the annotation to spot errors where you pass the wrong argument type.

I would write your code like this.
with open("myOutFile.txt", "w") as outF:  # Will close file automatically  when loop ends
    total = 0 
    for _ in range (3):  # Get three words
        scan_input = input("Enter line: ")
        scan_length = len(scan_input)  # Get word length
        total += scan_length  # Get total length so far
        outF.write(f'{scan_input}\nChar count: {scan_length}\n')
        print (" ", scan_input, scan_length)
 
print ("How many lines of text: 3")  # We know how many.  No need to count
print (f"How many characters of text (total chars): {total}")