Python not returning output [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
def get_prime_factors(N):
factors = list()
divisor = 2
while (divisor <= N):
if (N % divisor) == 0:
factors.append(divisor)
N = N//divisor
else:
divisor += 1
return factors
I'm trying to run this small project however I'm not getting any output either from my python IDLE shell, terminal or VSCode. any errors in my code?
I made a test script and it outputted normal, not sure why this one has no output.
running Python 3.10.6
on MacBook Pro M1
thanks!

Once you've defined the function, you must call it then print the result. Like this:
def get_prime_factors(N):
factors = list()
divisor = 2
while (divisor <= N):
if (N % divisor) == 0:
factors.append(divisor)
N = N//divisor
else:
divisor += 1
return factors
# get an input
n = input("Enter a number: ")
# call the function and assign return to a variabe
prime_factors = get_prime_factors(int(n))
# print the result
print(prime_factors)
You probably also want to prompt the user for input, so I've included code to do that in my answer.
PS: I have another answer about why you have to do this. That answer might be helpful if you're a beginner.

You have to call the function to see an output.
print(get_prime_factors(3))

Related

What is the meaning of this code, how does the sum of a consecutive number work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
N = int(input())
count = N
x = range(1, N +1)
for i in x:
N = i + N
print(N - count)
can someone tell me and explain how this code works? I have been spending minutes looking at this and still cant figure out whats going on, the concept is called "sum of consecutive numbers"
I tried this code and for example when I do 100 it shows 5,050 i want to understand how.
the given code is unnecessarily complicated and confusing. her a reworked version with some comments:
N = int(input()) # get input and convert it to int
total = 0 # variable to hold the running total
for i in range(1, N + 1): # create numbers from 1 upto N using a range
total += i # build sum and store it in var total
print(total) # output total

Can anyone please help how to write this code on Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
Create a counter that takes in a number from the user, counts from 0 to that number, tells the user as it counts if each number is even or odd, then sums all the numbers together and prints the sum.
The following code and comments are a starting point. The text following each # is a comment, you will need to replace the comments with the necessary code:
count = 0
sum = 0
num = 0
# Ask user for a number
#All code lines that need to be inside the while loop, need to be tabbed over once.
while (count <= num):
# add count to sum
# Use the following if statement along with the remainder
# operator (modulus, %) to check if a number is even or odd.
if count % 2 != 0:
#print the number is odd
else:
#print the number is even
# update counter for the while loop exit condition
count = count + 1
#print the sum
Using for loop
count=0
sum=0
for i in range(0,n+1):
if (i%2==0):
print("Number is Even")
else:
print("Number is Odd")
sum+=i
print(sum)
Using While loop:
count=0
sum=0
while(count<n):
if(count%2==0):
print("Number is Even")
else:
print("Number is odd")
count+=1
sum+=count
print(sum)

If-else in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
Fix this:
for n in range(1,8):
print(n)
if(n>=2 & n<=5):
print("This Number is either 2,3,4 or 5\n")
else:
print("This Number is "+n)
Use and.
and is a Logical AND that returns True if both the operands are true whereas & is a bitwise operator in Python
for n in range(1,8):
print(n)
if(n >= 2 and n <= 5):
print("This Number is either 2,3,4 or 5\n")
else:
print("This Number is ", + n)
1. & is bitwise and operator while and is used for logical and operator.
2. You need to convert integer to string for concatenation.
Corrected code:
for n in range(1,8):
print(n)
if(n>=2 and n<=5):
print("This Number is either 2,3,4 or 5\n")
else:
print("This Number is "+str(n))

Why is "s" doesn't return anything? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
The program is not returning anything, Why?
def g(n):
s=0
for i in range(1,n+1):
if n%i == 0:
s = s+1
return(s)
n = int(input("enter g value : "))
To return something you have to define a function.
You can write it as following
def Func(n):
s=0
for i in range(1,n+1):
if n%i == 0:
s = s+1
return(s)
Then you can use the following to get the value of n
n = int(input("enter g value : "))
value_of_s = Func(n)
print(value_of_s)

Can This Solution to the Google Foobar "Re-ID" be more efficient? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm looking for guidance on a better way to solve Google's Re-ID problem. Instead of typing out the "Re-ID" description here, I will simply list the pertinent parts:
There are 10,000 "Minions" each of which are assigned an integer randomly between 0 and 10000 inclusive.
The Minion will look up his integer as an index in a string of concatenated prime numbers - i.e. 2357111317192329...
His new "ID" is generated by finding the index value in the string according to the Minion's integer assignment and concatenating the next 4 numbers with it - i.e. if the Minion's integer assignment is 3 his new ID would be 71113 (7 is the value at index 3 of the string, then we add the next 4 digits/characters)
To solve this I:
Utilized (thanks again, SO) a function to identify if a number is prime or not: is_prime()
Generated a string of all prime numbers concatenated together which facilitates a Minion with assignment "10000" plus the next 4 characters.
Created a function to generate the new ID based on the Minion's assignment and the string of concatenated values: answer()
import math
def is_prime(n):
if n % 2 == 0 and n > 2:
return False
return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
primes = ''
for i in range(2,21000,1):
if len(primes) < 10005:
if is_prime(i):
primes = primes + str(i)
else:
break
def answer(n):
re_id = primes[n:n+5:1]
return(re_id)
Areas of possible improvement
The 21000 in my for loop is completely arbitrary. By trial and error I found 21,000 facilitates a Minion integer assignment of 10000 plus the 5 additional digits/characters.
Is it necessary to create this long string of concatenated prime numbers before hand? Could it not be done dynamically based on the Minion ID?
Thanks for your insights. Happy Friday.
I have used this code to complete the challenge..
def solution(b):
bag = "2"
for num in range(0,20500):
if num > 1:
for j in range(2,num):
if (num % j) == 0:
break
elif len(bag) >= 10006:
break
elif j==num-1:
bag += str(num)
break
else:
continue
return bag[b:b+5]

Categories

Resources