Difference between use and placement of user input in the following 2 programs and why using user input just after defining user defined function is working all correct in Program 1 and throwing error in Program 2. While both programs contain if else conditionals after user input and after that the function is called.
Program 1:
def greatest():
n1=int(input("Enter Number 1: "))
n2=int(input("Enter Number 2: "))
n3=int(input("Enter Number 3: "))
if n1>n2 and n1>n3:
print(f"{n1} is the greatest number.")
elif n2>n1 and n2>n3:
print(f"{n2} is the greatest number.")
elif n3>n1 and n3>n2:
print(f"{n3} is the greatest number.")
greatest()
Program 2:
def maximum():
num1=int(input("Enter Number 1: "))
num2=int(input("Enter Number 2: "))
num3=int(input("Enter Number 3: "))
if num1>num2:
if num1>num3:
return num1
else:
return num3
elif num2>num1:
if num2>num3:
return num2
else:
return num3
res=maximum()
print(f"{res} is the greatest number of all.")
Instructor
Yogesh Chawla Replied on 21/04/2024
Both program worked for me.
I tried with 10,11 and 12 value and both gave me 12 as per the code written.
What values did you try the code with?
I tried random values as user input, it was giving me the following error:
TypeError: 'int' object is not callable python
Same way in the factorial function written below, when user input is being taken below the factorial function but before calLing the function, it gives correct output but when user input is taken just after defining the function, it raises an error: NameError: name 'n' is not defined.
Why it detects n correctly when we take user input as in porgram 1 and error when we take user input as in program2.
PROGRAM 1:
factorial=1
def fact():
if n<0:
print(f"F{n} must be >=0.")
elif n==0 or n==1:
return 1
else:
return (n)*(factorial*(n-1))
n=int(input("Enter a positive integer: "))
result=fact()
print(f"The factorial of {n} is {result}")
PROGRAM 2:
factorial=1
def fact():
n=int(input("Enter a positive integer: "))
if n<0:
print(f"F{n} must be >=0.")
elif n==0 or n==1:
return 1
else:
return (n)*(factorial*(n-1))
result=fact()
print(f"The factorial of {n} is {result}")
Instructor
Yogesh Chawla Replied on 22/04/2024
This is because n is declared inside the function and outside the function, 'n' is not known.
You can solve this like this
factorial = 1
def fact():
n = int(input("Enter a positive integer: "))
print("H11")
if n < 0:
print(f"F{n} must be >=0.")
print("H12")
elif n == 0 or n == 1:
print("H13")
return 1
else:
return (n) * (factorial * (n - 1)),n
result,n = fact()
print(f"The factorial of {n} is {result}")
Can you please expain this program and what is H11, H12 and H13
Instructor
Yogesh Chawla Replied on 22/04/2024
Ignore that, that was just used for printing. See this
factorial = 1 # variable initialization
def fact(): # method definition
n = int(input("Enter a positive integer: ")) #prompt using input built-in function and
#converting the value to int
if n < 0:
print(f"F{n} must be >=0.")
elif n == 0 or n == 1:
return 1
else:
#Returning multiple values from a method using tuple
##Return tuple so we can use below --- result, n = fact()
return (n) * (factorial * (n - 1)),n
#A Tuple is a comma separated sequence of items.It can be created
#with or without ().Tuples are immutable.
result,n = fact() # Since fact() is returning tuple above so we can use two variables here
#result and n because we are using the same below for printing purpose
print(f"The factorial of {n} is {result}")
This is not giving the correct result, I checked with 5! and 4!, output comes to:
5! = 20
4! = 12
one more thing here is, as its a recursion function, I beleive another error is
instead of return (n) * (factorial * (n - 1)).......it can be (n) * (fact(n - 1)).
Didnt understand the reason of mentioning n here as a tuple.
Please try if it give u correct output.