Python Forum

Full Version: Python homework / functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, i have an assignment for python, but i don't know how to start it, if someone helped me i would be really grateful. Since English is not my first language, there may be some errors in the translation of the assignment.

Different lines are stored in the file rows (in the sentences (upper and lower case), numbers, dots, comma, and space symbol). The algorithm writes into a single file the different words found (converted into lowercase letters), the next file - found numbers, and indicates the amount of their recurrence.

Please help me, i will be really thankful.

this is what i have so far:

def atidarymas():
    failas = open("duomenys.txt", "r")
    duomenys = failas.read()
    duomenys = duomenys.replace("\n", "")
    duomenys = duomenys.replace(".", "")
    duomenys = duomenys.replace(",", "")
    sar = duomenys.split()
    failas.close()
    return (sar)
    

sar = atidarymas()
hello, you are lithuanian as i see, i just done the first part that splits everything, next time use:
with open("filename.txt", "r") as file:
	text=file.read()
so that you don't have to close the file after reading it Wink

duomenys = "null"
def atidarymas():
    global duomenys
    with open("duomenys.txt", "r") as failas:
        failas = failas.read()
        failas = failas.lower()
        duomenys=failas
        duomenys = duomenys.replace("\n", "")
        duomenys = duomenys.replace(".", "")
        duomenys = duomenys.replace(",", "")
        sar = duomenys.split()
        return (sar)
    

sar = atidarymas()
print(sar)
it's not really optimised but it works Tongue

this should be what you need Big Grin
duomenys = "null"
def atidarymas():
    global duomenys
    with open("duomenys.txt", "r") as failas:
        failas = failas.read()
        failas = failas.lower()
        duomenys=failas
        duomenys = duomenys.replace("\n", "")
        duomenys = duomenys.replace(".", "")
        duomenys = duomenys.replace(",", "")
        sar = duomenys.split()
        return (sar)
    

sar = atidarymas()
print(sar)
counterdict = {i:sar.count(i) for i in sar}
print(counterdict)

inp = input("count word: ")
if inp in counterdict.keys():
    print(str(counterdict[inp]))
else:
    print("not in the dictionnary")

Output:
['lol', 'lol', 'lol', 'lol', 'you', 'you', 'you', 'are', 'are', 'are', 'lithuanian', 'lithuanian', 'lithuanian', 'lithuanian'] {'lol': 4, 'you': 3, 'are': 3, 'lithuanian': 4} count word: lithuanian 4