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
#1
Big Grin 
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
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple problem, how best to solve it? SuchUmami 2 683 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  Simple Question - ' defined as "a". ?' Ryan012 10 1,491 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 1,830 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,471 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  Floor approximation problem in algorithm gradlon93 3 901 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  A simple "If...Else" question from a beginner Serena2022 6 1,638 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,178 Jun-14-2022, 08:35 PM
Last Post: thesquid
  Simple arithmetic question ebolisa 5 2,007 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,238 Oct-04-2021, 05:18 PM
Last Post: snippsat
  Question on None function in a machine learning algorithm Livingstone1337 1 2,331 Mar-17-2021, 10:12 PM
Last Post: supuflounder

Forum Jump:

User Panel Messages

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