+ 1
Help me with printing given number
Given any 5 numbers(not in order) as inputs, how do I print only two numbers that I want?
6 Antworten
+ 5
ComputerGuru 》ShillingN ,
to get multiple values for the input in one go, we can also do it like this:
#try to use this input: 2 5 7 4 11 (numbers are separated by a space)
nums = list(map(int, input().split()))
print(nums)
# output will be: [2, 5, 7, 4, 11]
how is it working:
> we take the input as a string by using input()
> the input string will then be split at each space position by using split()
> each of these splitted tokens will be taken be the map() function, and an int conversion will by applied by using int
> finally the result of all these nested steps (the map object) will be converted to a list by using the list constructor
+ 2
</CODER>
you cannot use numbers as variable name.
ComputerGuru 》ShillingN
Yes there are lists in Python and it's easy to use them.
The following code will take 5 numbers from user input, then prints the first and last one.
numbers = []
for _ in range(5):
numbers.append(int(input()))
print(numbers[0])
print(numbers[-1])
0
</CODER>
Thanks
It seems you did but isn't there a short way of storing the inputs in like Lists or Arrays?🤷♂️