This is my code:
a,b=input().split()
for x in range(a,b):
print(list(x))
Why is this not working?
It says can't interpret str as an int.
And when I put it under int(), it says int() argument must be a string.
I want this:
Input:
2 6
Output:
3
4
5
6
This does exactly what you expect:
a,b=input().split()
for x in range(int(a)+1,int(b)+1):
print(x)
input() returns a string, and split() returns a list of strings. You have to convert them to integers before feeding them to range. Also, in print(x) x is already a number and there is no reason to convert it into a list (and it would not work, either).
Simply replace print(list(x)) with print(x) . I don't see any reason to use list() . You can also use range(a,b+1) instead of range(a,b) if you want the last number printed to be the same as b.
By default, input() accepts values as string and split() returns a list of strings. You need to convert a and b to int after input. Also, you need to get rid of list(x) and just print(x) since int in Python is not iterable.
The correct code is:
a, b = map(int, input().split())
for x in range(a + 1, b + 1):
print(x)
OR
a, b = [int(i) for i in input().split()]
for x in range(a + 1, b + 1):
print(x)
In both cases, for input 2 6, the output is
3
4
5
6
Related
Note: you have input where have 5 numbers:
Sample Input: 12 21 15 19 8
Sample Output: 12 8
x = input()
for i in x:
print(i)
need to use input() in program but loop takes the number 12 as 1 and 2. how to avoid this
input: 12
output: 1 2
In case you also want to turn the numbers into integers to use in your code, you can do:
x = list(map(int, input().split()))
which just casts each of the numeric strings created by split() to int types and puts them back into a list.
Calling the split() method separates a sentence into a list of words.
x = input().split()
for i in x:
print(i)
Python String split() Method
Method 1 : Error : ufunc 'add' did not contain a loop with signature matching types dtype('
x = numpy.array(x)
x_5= x + 5
Method 2 : Error : must be str, not int
x_5 = [x+5 for x in x]
Method 3 : Error : invalid literal for int() with base 10: '-0.081428368'
I tried to convert the x data first to integer
x_int = list(map(int, x))
x_5 = [x+5 for x in x]
method 4 : Error : 'numpy.float64' object is not iterable
x = numpy.array(x, dtype=float)
x = numpy.array(x)
x_piu_5= x + 5
Method 5 : Error : float object is not iterable
x_piu_5=[]
xfl=[float(i) for i in x]
x_piu_5[:] = [x + 5 for x in xfl]
Hi All
I am trying to add an integer number to my list which contains a lot of numbers like 0.00085695 , etc, and I have used two methods but I have been unsuccessful
Update 1 : Added method 4 , I have obtained the values I wanted, but the problem now is that it say the numpy.float is not iterable
Update 2 : Added method 5, Should I convert the float to string before iteration ?
The core of your problem is that your list x contains strings representing floating-point numbers. You need to convert those strings to float objects.
More precisely:
Method 1 can be fixed by using dtype=float, as suggested on the comments:
x = numpy.array(x, dtype=float)
x_5 = x + 5
Method 2 can be fixed by converting the items of x to float values before adding 5:
x_5 = [float(i) + 5 for i in x]
Method 3 can be fixed by using float instead of int, as your values are not integers but rather floating-point values:
x_float = list(map(float, x))
x_5 = [i + 5 for i in x_float]
Note that this solution is equivalent to method 2, just a bit slower and more space consuming, as you are creating an additional list.
Method 4 can be fixed by removing the spurious x = numpy.array(x) line. You will end up with the same code as method 1.
As for method 5, I suspect that x is not the usual list, but rather it's a float object.
Other than converting values to the correct type, another thing you should try is to use different variable names for different things. In your code snippets, you're using x both for your lists/arrays and for the elements of those lists. While it's not always strictly required, using different variable names would solve a lot of confusion and save you from many headaches!
I want to sort 2 numbers from greatest to least without using a sort function or an array. Eg. input 4 and 8 and output will be 84, I don't want any commas or spaces in between the numbers. Written in Python 3 please.
Given two numbers, from somewhere, x and y
print(str(max(10*x + y, x + 10*y)))
Feeding off of Shawn Steffey, add the int(input()) to make sure input request is in an integer, in the if statement you take the two ints, make them a string, add them together and it will print "ab" or "ba" depending on input.
a = int(input("Enter a number"))
b = int(input("Enter a number"))
if a >= b:
print(str(a) + str(b))
else:
print(str(b) + str(a))
This sounds homeworky, based on the requirements.
If it is always two numbers being "sorted", try writing a basic comparison function.
eg. (in pseudocode, not specifically Python):
if(a >= b)
print(a + b);
else
print(b + a);
You can use max and min like this:
a = 4
b = 8
res = '{}{}'.format(max(a, b), min(a, b))
print(res) # Output: '84'
I have a number n and I need to get from a user n variables in one line.
As far as i know, it's pretty easy to do if you know exactly how many variables you have.
*variables* = map(int, input().split())
But if I don't know how many variables there are, what should I do?
Also, I'm asked to put the result in array.
Unfortunately, I've just started learning Python, so I have absolutely no idea how to do it and can't show any code I've tried.
User input being taken as a space separated string:
1 2 3 4 5
This being the code you are dealing with:
map(int, input().split())
Stating you need a list, then just store it in a single variable:
inputs = map(int, input().split())
However, dealing with Python 3, you will end up with map object. So if you actually need a list type, then just call list on the map function:
inputs = list(map(int, input().split()))
Demo:
>>> inputs = list(map(int, input().split()))
1 2 3 4 5
>>> type(inputs)
<class 'list'>
>>> inputs
[1, 2, 3, 4, 5]
I am trying to read array elements as
4 #no. of elements to be read in array
1 2 3 4
what i have tried by referring other answers
def main():
n=int(input("how many number you want to enter:"))
l=[]
for i in range(n):
l.append(int(input()))
this works fine if i give input as
4 #no. of elements to be read
1
2
3
4
but if i try to give like
4 #no. of element to be read
1 2 3 4
I get error as:
ValueError: invalid literal for int() with base 10: '1 2 3 4'
Please help me with this
Since there's no input delimiters in Python, you should use split and split the input that you've received from the user:
lst = your_input.split()
Your first approach is OK, for the second way use this:
n=int(input("how many number you want to enter:"))
l=map(int, input().split())[:n] # l is now a list of at most n integers
This will map the function int on the split parts (what split gives) of the user input (which are 1, 2, 3 and 4 in your example.
It also uses slicing (the [:n] after map) to slice in case the user put more integers in.
The input() function returns a string that the user enters. The int() function expects to convert a number as a string to the corresponding number value. So int('3') will return 3. But when you type in a string like 1 2 3 4 the function int() does not know how to convert that.
You can either follow your first example:
n = int(input('How many do you want to read?'))
alist = []
for i in range(n):
x = int(input('-->'))
alist.append(x)
The above requires you to only enter a number at a time.
Another way of doing it is just to split the string.
x = input('Enter a bunch of numbers separated by a space:')
alist = [int(i) for i in x.split()]
The split() method returns a list of numbers as strings excluding the spaces
n = input("how many number you want to enter :")
l=readerinput.split(" ")