Python Forum
question about simple algorithm to my problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about simple algorithm to my problem
#2
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}")
Reply


Messages In This Thread
RE: question about simple algorithm to my problem - by deanhystad - Oct-04-2021, 11:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 206 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  A simple problem, how best to solve it? SuchUmami 2 733 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  Simple Question - ' defined as "a". ?' Ryan012 10 1,670 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 1,979 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,600 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  Floor approximation problem in algorithm gradlon93 3 963 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  A simple "If...Else" question from a beginner Serena2022 6 1,766 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,248 Jun-14-2022, 08:35 PM
Last Post: thesquid
  Simple arithmetic question ebolisa 5 2,084 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,375 Oct-04-2021, 05:18 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020