Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python programming Start a new file named defs.py Write the code to create a fun

ID: 3821915 • Letter: P

Question

Python programming

Start a new file named defs.py

                Write the code to create a function, that when called would ask the user how many lines they would like from the file. Read that many line in from bibfile.txt and print them. Test the code.

                Write a new function like the above, but this time would take all of the lines and write them to a file named out.txt.

                Write a function to append lines to a file. Do this in a loop that would allow the user to continue adding lines to a file until they enter the letter q.

                Open a new file named testClass.py. Create a class named ask. Have this class when called print the words “Good Morning”. Go back to the defs.py file and write a function that when called will instantiate a member of the ask class, you can call him fred, and then call the method to have fred print the phrase.

here is what I have so far I am stuck

i = int(input('Please enter how many lines do you want from bibfile to print: '))
f = open("bibfile.txt","r")
while i > 0:
   out = f.readline()
   print(out)
   i = i - 1
f.close()

def print():
   i = int(input('Please enter how many lines do you want from bibfile to print: '))
   f = open("bibfile.txt","r")
   w = open("out.txt", "w")
   while i > 0:
       out = f.readline()
       w.write(out + " ")
       i = i - 1
   f.close()
print()

def app():
   i = int(input('Please enter lines you would like to append to out.txt press q to end'))
   o = open("out.txt", "a")
   while i in "q":
      
app()

Explanation / Answer

from testClass import ask
def printXLines():
    i = int(input('Please enter how many lines do you want from bibfile to print: '))
    f = open("bibfile.txt","r")
    while i > 0:
        out = f.readline()
        print(out)
        i = i - 1
    f.close()

def printAllLines():
    f = open("bibfile.txt","r")
    w = open("out.txt", "w")
    for line in f:
        w.write(line)
    f.close()
    w.close()
print()

def check():
    fred=ask()
    fred.wish()

def app():
    f= open("bibfile.txt","r")
    o = open("out.txt", "a")
    for line in f:
        c=input("enter a letter. Press q to stop appending lines")
        if(c=='q'):
            break
        o.write(line)
    f.close()
    o.close()

class ask():
    def wish():
        print("Good Morning ")