Accepting an unknown number of variables using input() [duplicate] - python

This question already has answers here:
Get a list of numbers as input from the user
(11 answers)
Closed 6 years ago.
I am trying to create a bounded rectangle using a random number of points that are provided by the user. The reason this is difficult for me is because all the numbers must be accepted on only one line, I don't know how many variables the user will provide, and since I am accepting points, I must have the right amount (evens).
Here is a sample run:
Enter the points:
``>>>4 1 3 5 1 5 9 0 2 5
My primary question is how do I unpack a random number of points? And also, how do I pair the even points together?

In Python 2:
points = map(int, raw_input().split())
In Python 3:
points = list(map(int, input().split()))
Another method - list comprehension:
points = [int(p) for p in input().split()]
To pair x and y of the points together you can use something like pairwise() on the points list: see https://stackoverflow.com/a/5389547/220700 for details.

If they are read as a string, you can use the split() method, which will return a list, then use map() to convert the items of the list to integers:
points_input = raw_input("Enter the points:")
points = map(int, points_input.split())
print points
Notes
The result (points) will be a list of integers.
If you are using Python 3.x, you have to use the method input() instead of raw_input().

Related

Creating a list or range with all numbers up to a certain variable [duplicate]

This question already has answers here:
How do I create a list with numbers between two values?
(12 answers)
Closed 17 days ago.
So I am defining a function that takes in one variable R. Then I need to create a list of all integers from 0 to R (in the context of the problem R will always be positive).
EX) When I do
R=5
print(list(0,R))
I just get a list with 2 elements: 0 and 5, but I want 0,1,2,3,4,5
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
range(6) would return [0,1,2,3,4,5]

Basic math in python [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
The code below is giving me a return of 25 with inputs of 3 & 4. Obviously it should be 7. This is a problem for school and I can't edit the first 3 lines or the last one. What am I missing here?
total_owls = 0
num_owls_A = input()
num_owls_B = input()
num_owls_A = int(input())
num_owls_B = int(input())
total_owls = (num_owls_A + num_owls_B)
print('Number of owls:', total_owls)
input() returns input value as a string. So, you are basically concatenating strings not integers.
If you want to add them as numbers you need to convert them to numbers first like below
num_owls_A = int(input())
num_owls_B = int(input())
Again, this will create an error, if you input a non-numerical value, so you need to handle the exceptions in such case.

How to iterate through or access a map in python? [duplicate]

This question already has answers here:
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 2 years ago.
I am taking input from the user in single line string input. Trying to convert the input string into a list of numbers using the below-mentioned command:
This command returns a of type <map at 0x1f3759638c8>.
How to iterate or access this a?
Try simply doing the following -
a = list(map(int, input().split(' ')))
print(a)
> 5 4 3
[5, 4, 3]
You will get an error for the input that you have given i.e. 'Python is fun' because map will try to map the input to function int().
Let me explain:-
When you use input() , the input taken is in form of string.
Using split(' ') splits the string on every occurrence of space and a list will be created.
Value of that list will be ['Python','is','fun']
Now what map function does is, it applies int() on every element of the list. So basically what you are trying to do is int('Python'), which will give an error.
To avoid this use the following code snippet:
a = map(int, input().split(' '))
'1 2 3 4'
for i in a:
print(i)
Output of the above code will be
1
2
3
4

Is it possible to append spaces and a certain amount of spaces to a string? [duplicate]

This question already has answers here:
create a string of variable length in python
(3 answers)
Python Spaced Triangle
(4 answers)
Closed 6 years ago.
Here is my current code. The question is exactly in the title. (I'm looking to be able to align my triangle correctly)
def Triangle(height):
if height % 2 == 0:
print "Please enter a height that is odd"
else:
stringTriangle = "x"
for x in range(height):
print stringTriangle
for x in range(1):
stringTriangle+="xx"
To make a string containing n spaces, use " "*n. To concatenate two strings a and b, use a + b. To learn about other things that can be done with strings, see Python 2: Built-in Types: str and Other Sequence Types.
From there, you should be able to solve your homework problem by calculating how many spaces and how many asterisks you need in each line of the figure.

Algorithm for sorting a list in python does not work properly [duplicate]

This question already has answers here:
How to sort python list of strings of numbers
(4 answers)
Closed 8 years ago.
I am using the following function in order to sort a list in an increasing order. However, while my function works for lists such as: [1,5,6,9,3] or [56,43,16,97,45], it does not work for lists of the form: [20,10,1,3,50].
In such cases, the computer seems to consider that 3>20 and 3>10 and 3 ends up right before 50 (second to last) in the "sorted" list I get. More precisely the result I get is: [1,10,20,3,50].
Here is my code:
def function_sort(L):
for j in range(len(L)):
min=j
for i in range(j+1,len(L)):
if L[i]<L[min]:
min = i
if(min != j):
L[j],L[min] = L[min],L[j]
print L
return L
Could anyone please explain me what is going on?
It sounds like your list consists of strings rather than integers, and you end up getting the elements sorted lexicographically.
By way of illustration, consider the following:
>>> 10 < 2
False
>>> '10' < '2'
True
To fix the issue, convert the elements to integers before sorting:
L = map(int, L)
P.S. I recommend against using min as a variable name since it shadows the built-in function min().

Categories