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()
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:
Count the number of occurrences of a character in a string
(26 answers)
Closed 10 months ago.
eg: "ABC.sample.int.int01" like that, only want to check this string contains 3 dots.
Thanks
You can check it like this, with count attribute of str in python. If it is equal to 3, then your strings actually contains exactly 3 dots.
if 'ABC.sample.int.int01'.count('.') == 3:
...
else:
...
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.
This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 5 years ago.
lets say I have a list a and a variable b = 1/len(a) but when I display the value of b it gives me 0
Because of integer divided by integer.
Try b=1.0/len(a)
This question already has answers here:
Pythonic way to check if a list is sorted or not
(27 answers)
Closed 6 years ago.
I have this list from user input:
sun=[8,8,7,7,4,6,4,5,]
>>> sun.index(8)
0
>>> sun.index(7)
2
>>> sun.index(4)
4
Would like to write a program to print "Impossible", because 4,5,6 are not in correct order in the list.
print "Impossible" * (sun != sorted(sun))