In python 3 I need to update the below script to produce the following output wh
ID: 3847458 • Letter: I
Question
In python 3 I need to update the below script to produce the following output when a value is input.
example output:
Enter a integer between 1 and 1,000: 246
The factors of 246: 2 3 41
or
Enter a integer between 1 and 1,000: 264
The factors of 264: 2 2 2 3 1
My code as it sits:
#Global list of the first 11 prime numbers
prime11 = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31)
#main function that asks users for number from 1 to 1000 and displays then prime factors
def main():
#get user input
num = int(input('Enter a positive integer between 1 and 1,000: '))
#call factor function to get and return prime factors
factors(num)
#Test cases
factors(12)
factors(82)
factors(600)
factors(566)
factors(197)
factors(864)
factors(954)
factors(321)
#factor finder function
def factors(num):
#variable to store user number chosen
n = num
#create a list for the prime factors to go
ftrs = []
#validate input
if n <=1000 and n >=1:
#loop through primes
for i in prime11:
# if one of them works
while n % i == 0:
#add it to list
ftrs.append(i)
#divide value and loop again
n = int(n/i)
#when value will no longer divide
if n % i != 0:
if n != 1:
#add it factor list
ftrs.append(n)
else:
pass
#clean the factor list up
ftrs.sort()
#print it out
print(' The factors of %d:' % num, str(ftrs)[1:-1], ' ')
#if bad data tell user
else:
print('Integer must be between 1 and 1,000.', ' ')
#call main
main()
Explanation / Answer
NOTE: I have modified your code so it can print the output in required format
Code:
#!/usr/local/bin/python3
#Global list of the first 11 prime numbers
prime11 = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31)
#main function that asks users for number from 1 to 1000 and displays then prime factors
def main():
#get user input
num = int(input('Enter a positive integer between 1 and 1,000: '))
#call factor function to get and return prime factors
factors(num)
#factor finder function
def factors(num):
#variable to store user number chosen
n = num
#create a list for the prime factors to go
ftrs = []
#validate input
if n <=1000 and n >=1:
#loop through primes
for i in prime11:
# if one of them works
while n % i == 0:
#add it to list
ftrs.append(i)
#divide value and loop again
n = int(n/i)
#when value will no longer divide
if n % i != 0:
if n != 1:
#add it factor list
ftrs.append(n)
else:
pass
#clean the factor list up
ftrs.sort()
#print it out
print(' The factors of %d: ' % num, end='')
for i in range(0, len(ftrs)):
print(ftrs[i], end=' ')
print(' ')
#if bad data tell user
else:
print('Integer must be between 1 and 1,000.', ' ')
#call main
main()
Execution and output:
Unix Terminal> python3 primef.py
Enter a positive integer between 1 and 1,000: 246
The factors of 246:2 3 41
Unix Terminal> vi primef.py
Unix Terminal> python3 primef.py
Enter a positive integer between 1 and 1,000: 246
The factors of 246: 2 3 41
Unix Terminal> python3 primef.py
Enter a positive integer between 1 and 1,000: 264
The factors of 264: 2 2 2 3 11
Unix Terminal> python3 primef.py
Enter a positive integer between 1 and 1,000: 100
The factors of 100: 2 2 5 5
Unix Terminal> python3 primef.py
Enter a positive integer between 1 and 1,000: 299
The factors of 299: 13 23
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.