Error in function max() for 3 number in python [duplicate] - python

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I'm beginner in python but I don't understand a thing.
This is the Code:
a = input("Insert first number ")
b = input("Insert second number ")
c = input("Insert third number ")
print("Max number is", max(a, b, c))
For example, I write at prompt:
Insert first number 12
Insert second number 34
Insert third number 100
Max number is 34
I don't understand! Please answer me!

Input returns string and strings are compared lexicographically. You should cast all input results to int, to get numbers.

Related

Comparision Operators issue [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
a=input("Enter the first Number ")
b=input("Enter the next number ")
c=a>b
print("Is a greater than b ? ", c )
ISSUE is it showing the opposite output always like the when you enter a greater than b it showing flase and vice versa
your problem is that you compare strings instead of floats
since when you are comparing stings python compares the lexicographic value of the strings, that way "9" is grater then "12357645"
if you convert the input to float that should fix it :)
a=input("Enter the first Number ")
b=input("Enter the next number ")
c=float(a)>float(b)
print("Is a greater than b ? ", c )

Basic math in python [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
The code below is giving me a return of 25 with inputs of 3 & 4. Obviously it should be 7. This is a problem for school and I can't edit the first 3 lines or the last one. What am I missing here?
total_owls = 0
num_owls_A = input()
num_owls_B = input()
num_owls_A = int(input())
num_owls_B = int(input())
total_owls = (num_owls_A + num_owls_B)
print('Number of owls:', total_owls)
input() returns input value as a string. So, you are basically concatenating strings not integers.
If you want to add them as numbers you need to convert them to numbers first like below
num_owls_A = int(input())
num_owls_B = int(input())
Again, this will create an error, if you input a non-numerical value, so you need to handle the exceptions in such case.

What is wrong in this Python Sum Program? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
a=input("Enter First Number ")
b=input("Enter second number ")
c=a+b
print("The Sum of two numbers are ",c)
"Why This Program not printing Sum of tho number?s?""
You need to cast the numbers to int. When you take an input, it is always a string. To cast a string to int, you can do int(a)+int(b)

Program printing a number a certain times instead of multiplying [duplicate]

This question already has answers here:
Why does multiplication repeats the number several times? [closed]
(7 answers)
Closed 1 year ago.
I’ve written a piece of code that instead of print the product, prints a number a certain number of times. Whats wrong with it?
twelve = 12
name = input("What is your name? \nAnswer: ")
print("Cool name!")
nums = input("\n\nHow much pocket money did you receive last month?\nAnswer: ")
total = nums * twelve
print("\n\nI think you get ", total + " pounds in pocket money per year! Nice!")
The reason is that your nums variable is a string, which is the default with all Python inputs. Try converting it to int:
nums = int(input(...))
Or float if you are inputting a floating point number.

input() function not working in Sublime text [duplicate]

This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
Closed 2 years ago.
I have the following function:
years=2
double_rate=(years*12)//3
number_of_rabbits=11
answer=number_of_rabbits*double_rate
print(answer)
I want to generalize the code by being able to input years and number_of_rabbits variables
years=input("select number of years: ")
print(years)
double_rate=(years*12)//3
number_of_rabbits=input("select number of rabbits: ")
print(number_of_rabbits)
answer=number_of_rabbits*double_rate
print(answer)
However the editor (sublime text) only prompts me for the first input variable. I am not able set the second one, "select number of rabbits", nor does it print the new answer
Does anyone know why this is the case?
The number of years you enter is saved as a string, not an int (or float). So when you try to calculate double_rate, you're multiplying a string by 12 (which is fine) and then floor dividing the result by 3, which doesn't work.
Try years = int(input("Select number of years: ")) instead.

Categories