You are receiving your input incorrectly. Try this:
a = []
for i in range(n):
a.append(int(input()))
Or, as a list comprehension:
a = [int(input()) for i in range(n)]
Also, if you want to test if a number can be split into two even numbers, try x % 4 instead of x % 2.
Python-Take multiple inputs using for loop and print multiple outputs through if condition - Stack Overflow
How to store multiple user inputs into a string during a while loop?
How to take more inputs from a user at the same time in Python?
Python using while loop with multiple inputs and choices - Stack Overflow
You are receiving your input incorrectly. Try this:
a = []
for i in range(n):
a.append(int(input()))
Or, as a list comprehension:
a = [int(input()) for i in range(n)]
Also, if you want to test if a number can be split into two even numbers, try x % 4 instead of x % 2.
for i in range(n):
a = list(map(int, input().rstrip().split()))
Each time through this loop, you're reassigning a, discarding its previous value.
So I am trying a practice problem with the following guidelines:
"Use while to get the user input, animal_name, of 4 animals use a counter, num_animals, in the while loop condition
append the names to a string variable, all_animals
User can exit early by typing "exit" (check if animal_name is "exit" and break)
when the loop finishes, print the names of all_animals"
My current code is:
num_animals = 0
while num_animals <4:
animal_name = input("Name an animal").lower()
num_animals += 1
all_animals = animal_name
if animal_name == "exit":
break
print(all_animals)Basically my question is, how can I get all 4 responses of animals to store in a variable so I can print that variable and have all 4 responses show up?
Hi!
How I can take more inputs (integers) from a user at the same time? I know that I need to use a list and .split(), but I dont know how.
Functionally the two approaches should be the same; I don't think there's any input you can give that will cause them to behave differently.
The main difference is that technique #2 involves code duplication (you had to copy and paste the input statement), which is bad because if you ever need to change the prompt now you need to change it in two places.
You can slightly shorten the code for technique #1 by not initializing name_in before the loop, since it's never used before you assign it within the loop body.
Python 3.8 introduced the walrus operator :=
# Loop-and-a-half (see https://www.python.org/dev/peps/pep-0572/#capturing-condition-values)
while (name_in := input('enter name or "done"')) != "done":
print(name_in)
isn't it a beauty?
You can ask for a single input and then split it into words with the str.split() method
# Get the input
nums = input('Enter numbers :')
# Split and map to `int`
nums = nums.split()
nums = list(map(int, nums))
You can append it first into a list
def myfunction(n):
return [input('Enter numbers: ' for _ in range(n - 1)]
numbers = myfunction(5)
Enter numbers: 1
Enter numbers: 2
Enter numbers: 3
Enter numbers: 4
And then, you can use " ".join(numbers)
result = " ".join(numbers)
print(result)
1 2 3 4
If you do not even want to have the space:
result2 = "".join(numbers)
print(result2)
1234