Answer to Question #306047 in Python for Michael

Question #306047

 Write a python function Read_Prime(fname, list)  which takes a list of numbers as an input, use this number to write them in a file "primes.txt" . The function then read the file and return a list containing only prime numbers as shown in the example. Also, write the exception handling code to show the proper message.


def Read_Prime(fname, list_1):

    #write your code here

 

    return

#Call your function from main

list_1=[1,2,3,4,5,6,7]

fname = "primes.txt"

res = Read_Prime(fname, list_1)

print(res)


Example-1

Example-2

Example-2

Input:

primes.txt

[1,2,3,4,5,6,7]

 

Output:

[1,2,3,5,7]

Input:

primes.txt

[3,6,5,13,18,21]

 

Output:

[3,5,13]

Input:

primes.txt

[2,4,6,8,10]

 

Output:

[2]



1
Expert's answer
2022-03-04T12:31:36-0500
'''
    Write a python function Read_Prime(fname, list)  which takes a list of numbers as an input,
    use this number to write them in a file "primes.txt" .
    The function then read the file and return a list containing only prime numbers as shown in the example.
    Also, write the exception handling code to show the proper message.


Input:
primes.txt
[1,2,3,4,5,6,7]


Output:
[1,2,3,5,7]


Input:
primes.txt
[3,6,5,13,18,21]


Output:
[3,5,13]


Input:
primes.txt
[2,4,6,8,10]


Output:
[2]
'''


outfile  = open("prime.txt","w")
s = (str(input("Enter numbers separated by space: "))).split(" ")
nums = []
for r in range(0,len(s)):
    nums.append(int(s[r]))
    outfile.write("%d\n"%(nums[r]))
outfile.close()


outfile  = open("prime.txt","r")
s = (outfile.read()).split("\n")
outfile.close()


u=" "
for r in range(0,len(s)-1):
    t = int(s[r])
    Flag=1
    for c in range(2,t//2):
        if(t%c==0):
            Flag=0
    if(Flag): u = u + str(t) + " "
print("Output:",u)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS