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]
'''
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)
Comments
Leave a comment