It shows the error as 'fact' not defined.why? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
def factorial(n):
if(n == 1):
fact = 1
return fact
else:
fact = a * factorial(n - 1)
return fact
a = int(input("Enter a number to find its factorial: ")
if (a > 0):
print("The factorial of", a, "is:", fact)
else:
print("Enter a positive value to find its factorial")
In the above code it tells me that - NameError name 'fact' is not defined .

Lines starting from a = int... should be outside your function. Once that is done all you need is to add fact = factorial(a).

Find the correct logic below.
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))

Related

max function not returning right ouput [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
(I am pretty new to python)
Hey guys, so I was trying to code a simple python program which finds the HCF of two numbers, but I am getting an error when the two numbers are co-prime and the actual HCF is not showing for the two numbers
(Ex: The HCF for 12 and 24 should be 12 but python is showing 4
Here is the code, can any of you guys help me?
factors = []
print("Enter first number")
num1 = int(input())
print("Enter second number")
num2 = int(input())
if num1 < num2:
lesser = num1
else:
lesser = num2
for i in range(2, lesser + 2):
if num1 % i == 0 and num2 % i == 0:
factors.append(i)
if not factors:
print(num1, "and", num2, "are co-prime numbers, so they don't have any common factors")
str_factors = ', '.join(map(str, factors))
hcf = max(str_factors)
print(hcf)
You are turning factors into a comma separated string and using max on the string. It's giving you the max of the string lexicographically. You need to use max on the factors variable.
Say for a string '2, 12, 42, 13' max will return '4' because it loops through all characters in a string
Fix this line and it should produce you expected output
hcf = max(factors)

TypeError: 'int' object is not callable (While working on google colaboratory) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
In python I got a "TypeError: 'int' object is not callable" during execution.
I had read other posts but I still can't figure out why it is like this.
# Python code to find if a number is
# prime or not using divmod()
# Given integer
n = int(input("Enter a number"))
x = n
# Initialising counter to 0
count = 0
while x != 0:
p, q = divmod(n, x)
x -= 1
if q == 0:
count += 1
if count > 2:
print(n, 'is Not Prime')
else:
print(n, 'is Prime')
And, please note that it doesn't gives any error by replacing:
n = int(input("Enter a number")) to n = int(input("Enter a number"))
I have also provided a screenshot regarding my problem 👇
image
Answer if anybody knows, Appreciation for any suggestions and comments.
Have you tried something like this?
z = input("Enter a number")
n = int(z)
x = n

How do i stop my code from outputting negative numbers? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to create a game of last man standing in python, but when you input a number it outputs the negative version of the correct answer? Also, when i enter anything other than 1, 2, or 3, I get the error: ValueError: could not convert string to float: '(anything other than the numbers)'
any help is appreciated. Thanks!
import random
num = random.randrange(20, 30)
print ("The number is " + str(num) + ", take either one, two or three away from it!")
take = float(input("input either 1, 2 or 3: "))
newnum = take - num
if take == 1:
print(newnum)
elif take == 2:
print(newnum)
elif take == 3:
print(newnum)
else:
print("please enter either 1, 2 or 3!")
You could just use a while loop to make sure the user only inputs 1,2 or 3.
import random
num = random.randrange(20, 30)
print ("The number is " + str(num) + ", take either one, two or three away from it!")
take = None
while take not in {1,2,3}: #{} faster than ()
take = int(input("input either 1, 2 or 3: "))
print("please enter either 1, 2 or 3!")
print(num-take)
Also as the above code shows, you need to use num-take instead of vice-versa.

How to fix a while syntax error in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm getting a syntax error on my 2nd while loop. Can't figure out why, any help appreciated :)
#intro
print("Welcome to my prime number detector.")
print("Provide an integer and I will determine if it is prime.")
#again loop
again = "Y"
while again == "Y":
num = (int(input("Enter an integer"))
#check for valid input
while num < 1:
num = (int(input("Enter an integer"))
#test for prime
for d in range(2,num):
if (num % d) == 0:
print(num,"is not prime.")
else:
print(num,"is prime.")
#ask again
again = intput("Do you want to play again? (Y/N)")
You are missing a closing parenthesis ) in the two of your following lines. The correct line of code is
num = (int(input("Enter an integer")))
Also, as sheepez mentioned below, your outer brackets are redundant. You can simply use
num = int(input("Enter an integer"))

Why does this loop only once? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
while count > 0:
if count = 0:
return n
elif count < 0:
print(" ") # prints empty if n is below 0
else:
count = count - 1
collect += math.ceil((n - 5)/2)
return collect
The inputs are (1003, 3) - result is 499, which means it just loop once and subtracts 5 and then divides by 2, then it stops. Anyone know why?
Your inner conditionals don't make sense with the while. And you have a return statement in the loop, so yes, it only looped once.
Start with this
import math
n, count = (1003, 3)
print("N = " + str(n))
while count > 0:
n = math.ceil((n - 5) / 2) # Update this to do whatever your logic is
print(count, n)
count -= 1
if n < 0:
print("N is negative")
else:
print("N = " + str(n))
You have several problems.
First, your syntax isn't indented evenly.
Second, your if statement have = instead of ==. The first one for assigning values to variables, the second is for checking equality.
Third, you have a return statement that's going to exit from whatever function this loop is inside.

Categories