Python Forum
Python homework / functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python homework / functions
#1
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()

Attached Files

.py   programa.py (Size: 322 bytes / Downloads: 295)
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HELP in python homework makashito 4 3,864 Oct-12-2021, 10:12 AM
Last Post: buran
  CyperSecurity Using Python HomeWork ward1995 1 1,931 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 2,257 May-09-2021, 11:19 AM
Last Post: Yoriz
  Homework with python Johnsonmfw 1 1,660 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  Python Homework Help *Urgent GS31 2 2,538 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Homework, functions janoshz 8 2,912 Nov-02-2019, 01:14 AM
Last Post: ichabod801
  Python Homework Question OrcDroid123 1 2,342 Sep-01-2019, 08:44 AM
Last Post: buran
  python homework help ASAP gk34332 1 2,944 Mar-13-2019, 07:27 PM
Last Post: ichabod801
  Python homework assigment makisha 3 3,232 Feb-28-2019, 10:21 PM
Last Post: Yoriz
  Python Homework Help beepBoop123 2 3,004 Dec-12-2018, 06:25 PM
Last Post: beepBoop123

Forum Jump:

User Panel Messages

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