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


Messages In This Thread
question about simple algorithm to my problem - by jamie_01 - Oct-04-2021, 01:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 234 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  A simple problem, how best to solve it? SuchUmami 2 752 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  Simple Question - ' defined as "a". ?' Ryan012 10 1,711 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 2,031 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,663 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  Floor approximation problem in algorithm gradlon93 3 989 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  A simple "If...Else" question from a beginner Serena2022 6 1,801 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,275 Jun-14-2022, 08:35 PM
Last Post: thesquid
  Simple arithmetic question ebolisa 5 2,124 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,427 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