I am new to Python and so I am a little unaware of the advanced functions. Here's how I approached this problem by breaking it into two .
First I found the factors of this number.
a = int(input("Enter Number to find Factors: "))
b = 1
while a >= b :
if a % b == 0 :
print(b)
b =1
Second, I prepared the psudo code for counting numbers
b = 17
count = 0
while b > 0 :
rem = b % 10
b = b // 10
count = 1
print(count)
can you guys help me how should I proceed like how should I join these I think I am having problem with syntax
CodePudding user response:
Modify your first solution to increase the count each time one is found.
a = int(input("Enter Number to find Factors: "))
b = 1
count = 0
while a >= b :
if a % b == 0 :
count = 1
print(b)
b =1
CodePudding user response:
Is this what you were asking for?
def get_factors(n, list):
b = 1
while n >= b :
if n % b == 0 :
list.append(b)
b =1
def count_digits(n):
count = 0
while n > 0 :
n = n // 10
count = 1
return count
n = int(input("Enter Number to find Factors: "))
# getting the list of factors
factors = list()
get_factors(n, factors)
# printing the factors and the amount of digits in each factor
for f in factors:
print("Factor:", f, ", Digits:", count_digits(f))
Every time you want to join more than one piece of code you can create a function using:
def function_name(parameters)
Then put your code inside the function or functions and you can apply the functions to whatever variables you want.
You can create functions that don't return anything. Those can be used like this:
function_name(parameters)
Or you can create functions that return values and they can be used like this:
a = function_name(parameters)
For more information you can check w3school they explain it well: https://www.w3schools.com/python/python_functions.asp
CodePudding user response:
In this answer I show how to connect the codes without using def.
You can just copy the second code and paste it inside of the if of the first one:
a = int(input("Enter Number to find Factors: "))
b = 1
while a >= b :
if a % b == 0 :
b2 = b
count = 0
while b2 > 0 :
rem = b2 % 10
b2 = b2 // 10
count = 1
print(b, count)
b =1
remember to copy the value of b to a different variable.