This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I tried this:
a = input()
print("+")
b = input()
c =a+b
print(c)
But it output 55 instead of 10 when I input 5 + 5
You are concatenating strings instead of adding integers, you can use int() to convert from string to integer and str() to convert from integer to string.
If you run print(5+5) you will get 10.
If you run print('5'+'5') you will get 55 because the + operator will concatenate strings together.
Related
This question already has answers here:
multiplication of two arguments in python [duplicate]
(6 answers)
Getting issue while trying to multiply string and integer [duplicate]
(2 answers)
Closed 3 months ago.
I want to output 5 * 2 = 10 but python output is 55!
How do I resolve this problem?
a = 0
b = 2
a = input("a? :") #(get 5 as input)
c = a * b
print (c)
This is my code.
when I input a number it repeat same number I entered two times insterd of showing multipiy it. What do I have to do to solve this?
a is a string,
so it will be
'5'*2='55'
if you want 10, you need to cast a to int.
a=int(input())
here is the link to document
https://docs.python.org/3/library/functions.html#input
This question already has answers here:
String of numbers to a list of integers?
(4 answers)
Closed 5 years ago.
The following block of code was tested on python 2.
A="1 2 10"
A=raw_input.split()
print A
This prints a list with 4 numbers(splitting the 10 into 1 and 0),
Why does this happen?
Just use normal split
B = A.split()
This question already has answers here:
Why does map return a map object instead of a list in Python 3?
(4 answers)
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 5 years ago.
How to Convert a string like '123' to int 1,2 and 3 so that I can perform 1+2+3. I am new to python. Can you please help me? I am not able to split the list. I don't think splitting the string will be of any use as there are no delimiters. Can you help me to understand how can this string elements be separated and treated as intergers?
x = "123"
s = 0
for a in x:
s = int(a) + s
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)
This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 7 years ago.
The length of the string needs to be 5 characters. When the string is "1" it needs to be returned as "00001", when the string is "10" it needs to be returned as "00010" and so on. I'm wondering how to do this using loops?
If you want to use for-loops, you can solve the problem like so:
def addPadding(str):
output = ''
# Prepend output with 0s
for i in range(5 - len(str)):
output += '0'
output += str
return output
print(addPadding('10'))
>> 00010
print(addPadding('1'))
>> 00001
If you can't use string formatting or arrays or anything besides integer operators, you should be able to figure it out using division and a loop.
Is 10 divisible by 10000?
Is 10 divisible by 1000?
Is 10 divisible by 100?
etc.
Try typing 10/10000 in your python interpreter. What's the result? :)