+ 1

For what this is used for?

input()

24th Jun 2020, 3:35 PM
Mohammad Bilal
Mohammad Bilal - avatar
5 Réponses
+ 1
This is use for taking input from user.
24th Jun 2020, 3:35 PM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar
+ 1
lst = []    n = int(input("Enter number of elements : ")) for i in range(0, n):     ele = int(input())     lst.append(ele) print(lst)
24th Jun 2020, 3:38 PM
Mohammad Bilal
Mohammad Bilal - avatar
0
In this
24th Jun 2020, 3:38 PM
Mohammad Bilal
Mohammad Bilal - avatar
0
How is it works
24th Jun 2020, 3:38 PM
Mohammad Bilal
Mohammad Bilal - avatar
0
Basically, the python code that you have provided is for inputting some elements into a list and then printing out this list. However, since 'int()' is present, the list is basically restricted to integers only while strings will yield an error and floats will be rounded to the nearest integer. Let me explain the usage of input() in your code: ### Python code ### lst = [] #<------ the list where you're going to append the integers. #Now, you are prompting the user to enter the number of digits he #wants to add to the list. n = int(input("Enter number of elements : ")) #Then, using the loop below, the user is again prompted to enter #integer value 'n times'(valued entered above) and then, this is # added to the list, lst. for i in range(0, n): ele = int(input()) lst.append(ele) #Finally, you print whatever has been input by the user in the #form of a list print(lst) By the way, he is an elegant python one-liner for your code: print([int(input()) for i in range(int(input()))]) Cheers
25th Jun 2020, 5:53 PM
UnknownSinister
UnknownSinister - avatar