Write a Python program to input a series of number from user and find Prime fact
ID: 3601783 • Letter: W
Question
Write a Python program to input a series of number from user and find Prime factors of each given number using a loop. Program will ask the user to continue or not each time and if user answers yes ask for the number otherwise stop the program. Hint: You need to use a nested loop.
Must include at least 3 functions.
Example
Do you want to find primes? (Y/N): y
Input any positive number: 10 Prime factors of 10 are 2, 5
Do you want to find primes? (Y/N): y
Input any positive number: A10
Invalid input must be a number.
Do you want to find primes? (Y/N): y
Input any positive number: -10
Inputted value must be a positive number
Do you want to find primes? (Y/N): y
Input any positive number: 125678
Prime factors of 125678 are 2, 7, 47, 191
Do you want to find primes? (Y/N): N
Have a nice day.
my code:
print("Do you want to find primes? (Y/N):", end="")
primes = input().upper()
while (primes=="Y"):
print("Input any positive number:",end="")
try:
pnum=int(input())
if pnum>=0:
pnum=pnum
else:
print("Inputted value must be a positive number")
except:
print("Invalid input must be a number.")
print("Do you want to find primes? (Y/N):", end="")
primes = input().upper()
print("Have a nice day.")
Explanation / Answer
Its good to see that you have tried the solution. Really appreciate it.
Here is the full solution. Kudos :D
def sqrt(num):
ret = num**(1/2.0)
return int(ret)
def printprime(num):
ret = False
for i in range(2, num + 1):
prime = True
if num % i == 0:
for j in range(2, sqrt(i)+1):
if i % j == 0:
prime = False
break
if prime:
print(i)
ret = True
if not ret:
print("No Prime Divisors")
print("Do you want to find primes? (Y/N):", end="")
primes = input().upper()
while (primes=="Y"):
print("Input any positive number:",end="")
try:
pnum=int(input())
if pnum>0:
ret = printprime(pnum)
else:
print("Inputted value must be a positive number")
except:
print("Invalid input must be a number.")
print("Do you want to find primes? (Y/N):", end = "")
primes = input().upper()
print("Have a nice day.")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.