Recursion output isn't printing - python

I'm new to python and trying to understand recursion. I'm trying to write a code where someone inputs 2 numbers (Num1, Num2). A calculation will take place until Num1 is greater than Num 2. The result of the calculation's final value should then be outputted.
This is my code:
def Recursive(Num1, Num2):
if Num1 > Num2:
return(10)
else:
if Num1 == Num2:
return(Num1)
else:
return(Num1+Recursive(Num1 * 2, Num2))
Num1=input("Enter number 1: ")
Num2=input("Enter number 2: ")
print("Final value: ", Recursive(Num1, Num2))
This is the output that comes out:
Enter number 1: 1
Enter number 2: 15
That's it. There's no output of my print statement. I'm confused as to what I'm doing wrong here and what I should do.

def Recursive(Num1, Num2):
if Num1 > Num2:
return 10
if Num1 == Num2:
return Num1
print(Num1, Num2)
return Num1 + Recursive(Num1 * 2, Num2)
Num1=int(input("Enter number 1: "))
Num2=int(input("Enter number 2: "))
print("Final value: ", Recursive(Num1, Num2))
This should be working code, the reason why your code was not working was because without the int in Num1 = int( input("") ) the program was reading the number as a string. As a result instead of getting 2 from Num1 * 2 when Num1 is 1 you got 11 as it was multiplying a string instead of an integer.
# Here is an example
a = input("Input a number: ")
b = int(input("input a number: "))
print(f"a is a:{type(a)}\n b is a:{type(b)}")
print(f"{a * 2}\n{b * 2}")
Copy the code above input the two numbers and it should give you a better understanding of what I just said.

Related

Printing an extra number

I'm asking the user to enter a number, then printing every even number up to the number entered. problem is if the inputed number is odd then the program prints an extra number.
What's happening?
num1 = int(input("Please enter a number : "))
num2 = 0
while num1 > num2:
num2 += 2
if num2 % 2 == 0:
print(num2)
else:
num2 -= 2
print(num2)
Let's settle this problem first step by step
If else statement is not needed
Your if else checks if num2 is even, but since num2 will always start at 0, and then be incremented by 2, it will always be even.
So this is what your code currently does :
num1 = int(input("Please enter a number : "))
num2 = 0
while num1 > num2:
num2 += 2
print(num2)
This should explain better why the program prints an extra number for an even input number.
Correct version
A very simple way to correct this code, would be to slightly change the while condition and add +1 to num2
num1 = int(input("Please enter a number : "))
num2 = 0
while num1 > num2 + 1:
num2 += 2
print(num2)
So here, it will print even numbers from 2 to num1
If num1 is even, it stops at num2 == num1 + 1
If num1 is not even, it stops at num2 == num1 + 2
num1 = int(input("Please enter a number: "))
num2 = 0
while num1 > num2:
if num2 % 2 == 0:
num2 += 2
if num2 > num1:
break
print(num2)

Python programming question - numbers and integers and division

Divisible challenge
Write a program that asks the user for two numbers. The program should output whether the two numbers are exactly divisible by each other. If not, it should return the remainder. If the user enters a 0 the program should give an error message.
so far I've done this:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
answer1 = num1/num2
answer2 = num2/num1
if num1/num2 == answer1(int) or num2/num1 == answer2(int):
print("Exactly divisible")
elif num1 == 0 or num2 == 0:
print("Error: you cannot divide by 0")
elif num1/num2 != answer1(int) or num2/num1 != answer2(int):
print("Not Exactly divisible")
please help...
I would approach this way
first check the zero case, in that case the code terminates faster
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
# answer1 = num1/num2
# answer2 = num2/num1
def divisible_by_each_other(num1, num2):
if (num1 == 0) or (num2==0):
print("Error do not enter 0")
#check divisibility
elif (num1%num2 == 0 ) or (num2%num1 == 0 ):
print("Exactly divisible")
else:
print("Not Exactly divisible")
high,low= max(num1,num2),min(num1,num2)
print("The remainder is ", high%low)
divisible_by_each_other(num1, num2)
answer1(int) is not the correct way to convert to an integer, it's int(answer1).
Since you already divided the numbers, you could use
if answer1 == int(answer1) or answer2 == int(answer2):
But you're making this much more complicated than it needs to be. Modulus is a simpler way to test divisibility.
if num1 % num2 == 0 or num2 % num1 == 0
You also should check if either of the numbers is zero before you try to use division or modulus. Otherwise, you'll get an error due to trying to divide by 0, which is what you're trying to prevent with this check.
If they're not divisible by each other, get the remainder of the larger divided by the smaller:
else:
return max(num1, num2) % min(num1, num2)
Here's the full, simplified code:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
higher = max(num1, num2)
lower = min(num1, num2)
if num1 == 0 or num2 == 0:
print("Error: you cannot divide by 0")
elif higher % lower == 0
print("Exactly divisible")
else:
print(higher % lower)

How to use input function while defining a function [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
def max(num1,num2,num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
elif num3 > num1 and num3 > num2:
return num3
elif num1 == num2 == num3:
return num1 == num2 == num3
else:
print("something went wrong.")
print(max(4,5,8))
I am a basic python programmer learning python,i have tried to define a function which prints out maximum of a three number,but how to use the input function to get the number as input.
you can just use something like
we are using int function because input function takes the number or string and makes it a string so we need integer version of them so we can compare them
num1 = int(input("what is your first number: "))
num2 = int(input("what is your second number: "))
num3 = int(input("what is your third number: "))
but it can give errors because you did not use try except so the proper way is
while True:
try:
num1 = int(input("what is your first number: "))
num2 = int(input("what is your second number: "))
num3 = int(input("what is your second number: "))
except:
print("give me integer or number or i do not know what type of data do you want")
else:
break
you can change what is in the last print statement
you can use that
it is still impovable you should check assert
i hope it is helpful

Find greatest number entered by four users

I am learning python. I need to know, is this also valid way to get an output?
Following code should give me highest user input( highest number among the four numbers)
num1 = int(input("Enter number 1: ")) #user 1
num2 = int(input("Enter number 2: ")) #user 2
num3 = int(input("Enter number 3: ")) #user 3
num4 = int(input("Enter number 4: ")) #user 4
allNum = {num1, num2, num3, num4}
print("All numbers: ",allNum)
if(num1 > num2 and num1 > num4) :
print("User 1's number is greater")
elif(num2 > num3) :
print("User 2's number is greater")
elif(num3 > num4) :
print("User 3's number is greater")
elif(num4 > num2) :
print("User 4's number is greater")
else :
print("done")
With your current method of directly comparing each number, you would do something like this:
if num1 > num2 and num1 > num3 and num1 > num4:
...
elif num2 > num1 and num2 > num3 and num2 > num4:
...
...
Luckily, there is an easier way! You can use Python's built-in max() function, which returns the maximum value of a sequence. That would look something like this:
maximum = max(num1, num2, num3, num4)
if num1 == maximum:
...
elif num2 == maximum:
...
...
The above method works fine, but what if you wanted to use 5 numbers? What about 6? 20? The more numbers you add, the more messy it gets. Each additional number of numbers, you would add another elif statement, which gets tedious. So I would also recommend using some loops and a dictionary for gathering, storing, and comparing the user input.
>>> nums = {}
>>> for i in range(4):
nums[i+1] = int(input(f"Enter number {i+1}: "))
Enter number 1: 4
Enter number 2: 2
Enter number 3: 8
Enter number 4: 10
>>> print("All numbers:", list(nums.values()))
All numbers: [4, 2, 8, 10]
>>> for k, v in nums.items():
if v == max(nums.values()):
print(f"Number {k} is greater")
break
Number 4 is greater
>>>
Trying to keep it as simple as possible for you to understand
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
num4 = int(input("Enter number 4: "))
#Store all numbers in list.
allNum = [num1, num2, num3, num4]
#Get the max value of all numbers in that list
maxNum = max(allNum)
#To get the position of this value, use list.index() function
maxIndex = allNum.index(max(allNum))
#Lets say your numbers are [3,5,4,1]. .index() would give you "1" because the index starts from 0 to length of list -1.
#In this case, it would be 0-3. So, if you want `Number 2` is greater instead, then you just add 1 to the maxIndex.
print("Greatest number is number "+str(maxIndex+1))
You can index and max to solve this.
a = []
for i in range(4):
a.append(int(input(f"Enter number {i+1}: ")))
print(f"Number {a.index(max(a)) + 1} is greater")
This is the code i wrote .. I'm just learning python
n1 = int(input("Enter 1st Number: "))
n2 = int(input("Enter 2nd Number: "))
n3 = int(input("Enter 3rd Number: "))
n4 = int(input("Enter 4th Number: "))
if (n1>n2) and (n1>n3) and (n1>n4):
print("n1 is Greater")
elif (n2>n1) and (n2>n3) and (n2>n4):
print("n2 is Greater")
elif (n3>n2) and (n3>n1) and (n3>n4):
print("n3 is Greater")
else:
print(n4,"is Greater")

Hello I was confused about this code because purely from a technical stand point it should work

This is supposed to take three int values, and give you the LOWEST amount
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 < num2) and (num1 < num3):
print(num1)
elif num2 < num3
print(num2)
else:
print(num3)
I know it's messy and kinda hard to read but my point is when i put something like 7 15 3, it always outputs 15. Why?
I reprodcued papke's result: adding a colon to the end of the elif line fixed the syntax error and produced the output you expected.
Taking advantage of the built-in min function, you could refactor your code along these lines:
num1 = int(input())
num2 = int(input())
num3 = int(input())
print(min([num1, num2, num3]))
You were missing a colon after the elif. The code gives the expected answer:
num1 = int(input("Enter: "))
num2 = int(input("Enter: "))
num3 = int(input("Enter: "))
if (num1 < num2) and (num1 < num3):
print(num1)
elif num2 < num3:
print(num2)
else:
print(num3)

Categories

Resources