-4
print("Enter length of elements You want to sort\t")
a=input()
print("Enter those elements")
b=raw_input()
c=len(b)
if(a==c):
    print max(b)
else:
   print("Entered number of elements doesn't matches with length")

Here variable c wants to store the number of elements that list b contains, so that it can compare with the length, and produce result.

But it is showing some error. I would like to know is there any other method available to assign length of list into a variable. In above program every time the output is displaying as

Enter length of elements You want to sort   
5
Enter those elements
1 2 3 4 5
Entered number of elements doesn't matches with length

Even though the length and Number of elements matches

piet.t
  • 11,718
  • 21
  • 43
  • 52
Durga Prasad
  • 11
  • 1
  • 6
  • 4
    `len(b)` not `b.len()`. – alkasm Jul 13 '17 at 07:12
  • 7
    Possible duplicate of [How to get the size of a list](https://stackoverflow.com/questions/1712227/how-to-get-the-size-of-a-list) – Chris_Rands Jul 13 '17 at 07:13
  • My Question was how to store the length of list into a variable, not to just know the length of the list. So I think My question can't be duplicate of How to get the size of a list -- Chris_Rands........ Anyway Thank You for comment, I got good information from that link too – Durga Prasad Jul 17 '17 at 06:20
  • Hi Piet, I have answered my Question below of the page, Why u are editing the Question again. Its not my fault to make you scroll u down the page. Anyway thanks for editing now. – Durga Prasad Aug 01 '17 at 09:15

3 Answers3

1

You need to use len() function to get length of a list, string..

>> a = [1, 2, 3, 4]
>> len(a)
  4

In your code you are using raw_input(). raw_input() takes input and returns it as string .. so you need to convert your input string in list.

print("Enter length of elements You want to sort\t")
a=input()
print("Enter those elements")
b=raw_input()
b1=b.split(',')
c=len(b1)
if(a==c):
    print max(b)
else:
    print("Entered number of elements doesn't matches with length")

then pass your input to raw_input as 1,2,3,4

viveksyngh
  • 777
  • 7
  • 15
  • Even though the length and number of entered elements matches, the result shows Entered number of elements doesn't matches with length. – Durga Prasad Jul 13 '17 at 08:33
0

You initialize your b variable with the result of the raw_input() function. Which is a string (https://docs.python.org/2/library/functions.html#raw_input)

Then you try to initialize c with the length of b. String objects have no "len" attribute, which is easily verifiable by running

dir(c)

The len() builtin function does what you need however and you can get the length of your raw_input() string by calling

answer_length = len(c)

However, calling that directly on your input string without first parsing it would result in you getting the length of the string as it was inserted by the user, not the number of elements you need to sort. To get each individual number to be sorted into a list you would need to do something like this:

input_list = [int(i) for i in b.split()]

What that line does is split the string you got in b (the result of your raw_input()) by the space character, then populate a variable named input_list with the integer value of each element of the split list.

Then you can apply a simple sort to it and get the sorted list right back

input_list.sort()

With your sorted list now you can get min/max, do other operations you might need.

BoboDarph
  • 2,751
  • 1
  • 10
  • 15
0

Python Program to find the largest number in a list using functions:

a=[]
d=0
n=input("Enter the length of the list")
def large(a):
    for i in range(1,n+1):
        b=input("Enter elements")
        a.extend(b)
        d=n-len(a)
        if(len(a)!=n):
            print("add",d,"elements")
        else:
            print "The largest number is:",max(a)
            exit()
large(a)

'''
Output:-
Enter the length of the list5
Enter elements1,2,3,4
('add', 1, 'elements')
Enter elements[5]
The largest number is:5   '''
Durga Prasad
  • 11
  • 1
  • 6