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
Related
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 1 year ago.
I tried to create a program which returns the number of times a certain string occurs in the main string.
main_string="ABCDCDC"
find_string="CDC"
print(main_string.count(find_string))
Output=1
....
But there are 2 CDC. Is there any other ways to solve?
Try using regex:
print(len(re.findall(fr"(?={find_string})", main_string)))
Or try using this list comprehension:
x = len(find_string)
print(len([main_string[i:x + i] for i in range(len(main_string)) if main_string[i:x + i] == find_string]))
Both codes output:
2
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:
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 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))