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)
Related
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 )
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.
This question already has answers here:
How to convert a negative number to positive?
(7 answers)
Closed 3 years ago.
After prompting a user to enter an integer and printing the absolute value of it, how do you convert a negative input integer into positive using if statements.
number is your integer input.
Code:
number = abs(number)
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.
This question already has answers here:
Convert int to ASCII and back in Python
(6 answers)
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
I have something like this right now but this wont work because chr requires an integer. How can I make it print the chr of the user input?
num1 = input("Enter a ASCII code 0 - 127:")
print(str(chr(num1)))
Just cast the user input to int first -
num1 = input("Enter a ASCII code 0 - 127:")
print chr(int(num1))
(Incorporated #chepner's comment)