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)
Related
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.
Write a program whose inputs are three integers, and whose output is the smallest of the three values.
If the input is:
7
15
3
The output is: 3
This is the code I have come up with:
num1 = input()
num2 = input()
num3 = input()
if (num1 < num2):
if (num1 < num3):
smallest_num = num1
elif (num2 < num1):
if (num2 < num3):
smallest_num = num2
else:
smallest_num = num3
print(smallest_num)
This code works for that input, however if you input "29, 6, 17" it returned no output with an error
NameError: name 'smallest_num' is not defined".
I have dinked around quite a bit and tried adding smallest_num = min(num1, num2, num3) however nothing has yielded a passing output.
The issue is that input() returns a string. So when you compare your variables, you're doing string comparisons instead of numerical comparisons. So, you need to convert your input to integers.
num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))
num3 = int(input("Enter num3: "))
print(min(num1, num2, num3))
This should work. I use C++, but it also looks like you didn't define smallest_num as a variable which could be the only problem.
if (num1 < num2 and num1 < num3):
small = num1
elif (num2 < num1 and num2 < num3):
small = num2
else:
small = num3
print(small)
In case anyone else needed help on this one in the future. :)
Original question: Write a program whose inputs are three integers, and whose output is the smallest of the three values.
If the input is:
7
15
3
The output is: 3
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 < num2 and num1 < num3):
small = num1
elif (num2 < num1 and num2 < num3):
small = num2
else:
small = num3
print(small)
num1 = int(input())
num2 = int(input())
num3 = int(input())
if (num1 <= num2 and num1 <= num3):
small = num1
elif (num2 <= num1 and num2 <= num3):
small = num2
else:
small = num3
print(small)
I am currently doing this lab for my own homework, and one thing that is missing from their responses is the definition of the variable that specifies which of the three values is the lowest/minimum value: lowest = min(num1, num2, num3)
num1 = int(input())
num2 = int(input())
num3 = int(input())
lowest = min(num1, num2, num3)
if num1 < num2 and num1 < num3:
lowest = num1
elif num2 < num1 and num2 < num3:
lowest = num2
elif num3 < num1 and num3 < num2:
lowest = num3
print(lowest)
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
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)
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I have tried to find smallest of three numbers but for some values the answer is wrong like 10,30,4 for example and it shows smallest number to be 10.
num1 = input("Enter a number: ")
num3 = input("Enter a number: ")
num4 = input("Enter a number: ")
if (num1 < num3) and (num1 < num4):
print(num1)
elif (num3 < num4) and (num3 < num1):
print(num3)
else:
print(num4)
You are comparing text. You need to convert the input to int objects before comparing.
num1 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
num4 = int(input("Enter a number: "))
if (num1 < num3) and (num1 < num4):
print(num1)
elif (num3 < num4) and (num3 < num1):
print(num3)
else:
print(num4)
Problem: input() returns a string, and strings are compared lexicographically. So you should convert it into an integer, you can do this with int(..):
num1 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
num4 = int(input("Enter a number: "))
if (num1 < num3) and (num1 < num4):
print(num1)
elif (num3 < num4) and (num3 < num1):
print(num3)
else:
print(num4)
But nevertheless this is not a good way to calculate the minimum. What will you do if you have to calculate the minimum of 50 integers? Construct a hug amount of checks? Computer scientist Edsger W. Dijkstra once said:
"Two or more? Use a for!" - Edsger W. Dijkstra
So in case you have to do work multiple times, you better use a for loop to handle that. The skeleton of such code fragment looks like:
for i in range(3):
# ...
pass
The body of the for loop (here ...) will be repeated three times. Now what work can we put into the for loop? Of course we can query the user for input. So we can write:
for i in range(3):
number = int(input("Enter a number: "))
# ...
but now we still have to calculate the minimum. A way to handle this is by using an accumulator: a variable that is maintained through iteration, and updated accordingly. Here an accumulator can be the thus far obtained minimum. Since initially we have no minimum, we can initialize it with None:
thus_far_minimum = None
for i in range(3):
number = int(input("Enter a number: "))
# ...
Now in case thus_far_minimum is None, we can assign the number we have queried from the user. Furthermore in case thus_far_minimum is no longer None (so it is initialized), but number is less than thus_far_minimum, we can alter thus_far_minimum such that it now has the minimum. For example:
thus_far_minimum = None
for i in range(3):
number = int(input("Enter a number: "))
if thus_far_minimum is None or number < thus_far_minimum:
thus_far_minimum = number
print(thus_far_minimum)
at the end we can print the thus_far_minimum since it is the minimum over all the numbers we have seen.
With input(), you get strings, so convert the input values to integers.
num1 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
num4 = int(input("Enter a number: "))
Set a variable to num1, then reset it as you compare num1 to num3 and num4.
smallest = num1
if num3 < smallest:
smallest = num3
if num4 < smallest:
smallest = num4
if num3 < num4:
smallest = num3
print(smallest)