Find biggest number out of 3 numbers - python

I am trying to find of biggest of 3 numbers using python3.6. But its returning wrong value.
#!/usr/bin/python3
a=input("Enter first number: ")
b=input("Enter second number: ")
c=input("Enter Third number: ")
if (a >= b) and (a >= c):
largest=a
elif (b >= a) and (b >= c):
largest=b
else:
largest=c
print(largest, "is the greatest of all")
If I provide, a=15; b=10 and c=9
Expected output should be 15.
But I am getting actual output as 9.

input() returns strings. You need to convert the strings to class int to compare their numeric value:
a = int(input("Enter the first number: "))
In string comparisons, 9 is greater than 1. You can try in the REPL:
>>> '9' > '111111111111111111'
True

You can use the max() builtin function of python:
https://www.freecodecamp.org/forum/t/python-max-function/19205

Like the comment of khelwood mentioned it You are comparing strings, not numbers. – khelwood. You should first cast the input values to integers (or float) and then compare them.
More about casting in python can be found here

As khelwood pointed out in comment those inputs are used as strings.
try inserting this after input and its fixed:
a = int(a)
b = int(b)
c = int(c)

Related

Understanding bools in Python [duplicate]

This question already has answers here:
Python input never equals an integer [duplicate]
(5 answers)
Closed 2 years ago.
I'm trying to learn some Python and had a question about a really small "program" I made as a test.
a = input()
print(a)
b = '10'
if a == b:
print("Yes")
else:
print("No")
This works, but my question is why does the value for b have to have the quotes around it.
Python input() function will by default take any input that you give and store it as a string.
why does the value for b have to have the quotes around it
Well, it doesn't have to have quotes. But if you need the condition to evaluate to True, then you need quotes. So, since a is a string, you need to have b = '10' with the quotation mark if you need a == b to evaluate to True.
If your input is an integer, you could also do a = int(input()) and in this case, b=10 would work as well. Simple !
So, the following two can be considered to be equivalent in terms of the result they give -
a = input()
b = '10'
if a == b:
print("Yes")
else:
print("No")
AND
a = int(input())
b = 10
if a == b:
print("Yes")
else:
print("No")
It is actually quite simple. All inputs from the user are considered as string values by python. In python, you can only compare a string to string, integer to integer and so on...
You could do this as well
a = int(input())
print(a)
b = 10
if a == b:
print("Yes")
else:
print("No")
Over here int() converts the value you enter to an integer. So you don't need quotes for the variable b
The value of input() is a string, therefore you must compare it to a string. Everyone learning programming starts off with many questions, and it is the only way to learn, so do not feel bad about it.
In python default input type is string. if you want it as int you must cast it:
a = int(input()) #change type of input from string to int
print(type(a))
b = 10
if a == b:
print("Yes")
else:
print("No")

I was trying to complete a palindrome-check but reverse variable is not having the expected value

The following code should perform a check if the number provided by a user is palindrome or not, but the code in while loop reverse is always not equal to the number even when I enter 12321 as a value.
number=input("Type the number:")
temp=number
reverse=0
reminder=0
while temp!=0 :
reminder=int(temp)%10
reverse=reverse*10+reminder
temp=int(temp)/10
if reverse==number :
print("this is a palindrome number")
else:
print("this is not a palindrome number")
You can solve it with small hacks: string representation of number and iterating with negative indices:
def is_palindrome(num):
string = str(num)
for i, e in enumerate(string):
if e != string[-1 * (i+1)]:
return False
return True
is_palindrome(12344321)
True
The below code should work. Any input entered is always a string. While comparing the result with the input number, convert it to int. Also do floor division ("//").
number = input("Type the number:")
temp = int(number)
reverse = 0
reminder = 0
while temp != 0:
reminder = temp % 10
reverse = reverse*10+reminder
print(reverse)
temp = temp//10
if reverse == int(number):
print("this is a palindrome number")
else:
print("this is not a palindrome number")
If you want a slightly better solution than all the dividing thing. Then the below code should work.
number = input("Type the number:")
if number.isdigit():
if number == number[::-1]:
print('this is a palindrome number')
else:
print('this is not a palindrome number')
else:
print('Input entered is not a number')
for floor division, use "//"
number=int(input("Type the number:"))
temp=number
reverse=0
reminder=0
while temp!=0 :
reminder=temp%10
reverse=reverse*10+reminder
temp=temp//10 # <-------
# or u can use
# temp=int(temp/10) # <-------
if reverse==number :
print("this is a palindrome number")
else:
print("this is not a palindrome number")
// floor division
Description:
Returns the integral part of the quotient.
Syntax:
A // B
A: Any expression evaluating to a numeric type.
B: Any expression evaluating to a numeric type.
Return Value:
According to coercion rules.
Remarks:
Also referred to as integer division. The resultant value is a whole
integer, though the result’s type is not necessarily int.
Example:
>>> 5.0 / 2
2.5
>>> 5.0 // 2
2.0
.
NOTE:
In Python 3.0, 5 / 2 will return 2.5 and 5 // 2 will return 2. The
former is floating point division, and the latter is floor division,
sometimes also called integer division.
In Python 2.2 or later in the 2.x line, there is no difference for
integers unless you perform a from future import division, which
causes Python 2.x to adopt the behavior of 3.0
Regardless of the future import, 5.0 // 2 will return 2.0 since that's
the floor division result of the operation.
i would change the line for the temp
temp=int(int(temp)/10)
and I would also ask whether int(number)== int(reverse)
This is another way to check in python if a number is palindrome
number = input("Type the number:")
temp = str(number)
reverse = temp[::-1]
if reverse == temp :
print("this is a palindrome number")
else:
print("this is not a palindrome number")

checking for decimal points using a while loop

I wrote some code to check if two input variables contained decimal points as so:
while "." in n:
print()
print("Please enter only integer numbers")
n = input("Please enter the numerator:")
d = input("Please enter the denominator:")
while "." in d:
print()
print("Please enter only integer numbers")
n = input("Please enter the numerator:")
d = input("Please enter the denominator:")
however, I need a way for these loops to be ignored if the decimal is followed by a 0, e.g. 4.0 or 8.0.
Is there a way to do this, or is there a better way to check if the number entered is a decimal? I don't want to make the input a float from the beginning as it also needs to work if the user enters an integer.
The version of python I'm using is 3.5.4.
this the solution you are looking for
while int(float(d)) != float(d) or int(float(n)) != float(n):
other answers won't work for numbers like 8.05
To piggy-back off of #public static void main 's answer,
you can also consolidate your loop.
while ("." in n and ".0" not in n) or ("." in d and ".0" not in d):
n = input("Enter the numerator: ")
d = input("Enter the denominator: ")
Replace this:
while "." in n:
with this:
while "." in n and ".0" not in n:
This makes the code in the loop execute when "." is found in n, but ".0" is not found in n.
Consider comparing them as the actual value types:
while int(float(d)) != float(d) or int(float(n)) != float(n):

Python: not all arguments converted during string formatting

This code gives an error
print('type a whole number:')
n = input()
if n % 2 == 1:
print('Odd');
else:
print('Even');
I'm assuming there's something special I have to do to variable n in the if statement? I am a beginner to Python.
Here is how to fix it:
n = int(input("type a whole number:"))
Since input() returns a string, you need to convert it to an int first, using int().
You need to convert n to an integer first, in py 3.x input() returns a string.:
n = int(input())
Convert the user input n to an integer first.
i.e. Simply Change :
n = input()
To :
n = int(input())
Also, input() can take a string as an argument, which is printed before taking the input.
So, you can change
print('type a whole number:')
n = int(input())
To
n = int(input('type a whole number:'))

Python ISBN program

I'm trying to calculate the check digit for an ISBN input on python. so far I have...
def ISBN():
numlist = []
request = raw_input("Please enter the 10 digit number: ")
if len(request) == 10:
**numlist == request
print numlist**
if len(request) != 10:
print "Invalid Input"
ISBN()
ISBN()
The bold bit is where im having trouble, I cant seem to split the 10 digit input into individual numbers in the list (numlist) and then multiply the seperated individual numbers by 11 then the next by 10 then the next by 9 etc...
For the next part of the program, I will need to add these new multiplied numbers in the list together, then i will use the mod(%) function to get the remainder then subtract the number from 11, any assistance with any of my coding or incorrect statements on how to calculate the ISBN would be greatly appreciated.
Thank you.
This code should get you on your way:
listofnums = [int(digit) for digit in '1234567890']
multipliers = reversed(range(2,12))
multipliednums = [a*b for a,b in zip(listofnums, multipliers)]
Strings are iterable, so if you iterate them, each element is returned as a single-character string.
int builds an int from a (valid) string.
The notation [a*b for a,b in zip(listofnums, multipliers)] is a list comprehension, a convenient syntax for mapping sequences to new lists.
As to the rest, explore them in your repl. Note that reversed returns a generator: if you want to see what is "in" it, you will need to use tuple or list to force its evaluation. This can be dangerous for infinite generators, for obvious reasons.
I believe list() is what you are looking for.
numlist=list(request)
Here is how I would write the code. I hope I'm interpreting the code correctly.
def ISBN():
request = raw_input("Please enter the 10 digit number: ")
if len(request) == 10:
numlist = list(request)
print numlist
else:
print "Invalid Input"
ISBN()
import itertools
if sum(x * int(d) for x, d in zip(nums, itertools.count(10, -1))) % 11 != 0:
print "no good"

Categories