0

I'm trying to understand this error of:

  float object has no attribute a

Here is a simplified version of my code:

    def Apple():
             a = input("first: ")
             b = input("second: ")

             list1 = [0..a];
             list2 = [0..b];

             print list1, list2

Here is how the error is given

    >> Apple()
    >> AttributeError: 'float' object has no attribute 'a'
7
  • Not sure if this was introduced in posting the question or part of your actual code, but your second input has a typo Commented Aug 12, 2013 at 19:12
  • 1
    What are you looking to do? If you're looking to produce ranges, then that's not how it's done in Python. Commented Aug 12, 2013 at 19:12
  • Basically. I've been trying ways to make lists out of parameters inputted by the user, and doing things with numpy to those lists. and then printing the results. But, I just can't seem to make the lists. Where is the error, Stephen? Commented Aug 12, 2013 at 19:15
  • @NilesBernoulli What gave you the idea that [0..a] would make a list? Commented Aug 12, 2013 at 19:16
  • @NilesBernoulli I was just pointing out that one of your inputs was an inout. It has since been changed. Joran and Apero seem to have an idea what the overarching issue is. Commented Aug 12, 2013 at 19:17

3 Answers 3

4

Since the poster asked about the error specifically:

I believe that in the line list1 = [0..a];

the python interpreter takes the expression 0..a, and parses it as the float 0. followed by a call to the a attribute of 0., which is usually what a dot means in that context.

As has been mentioned already, to create a range, use range(0, int(a)) instead.

Sign up to request clarification or add additional context in comments.

Comments

3

range(int(a)) is what you want I think, not [0..a]

on a side note input is very dangerous in py2x since it evaluates the input

4 Comments

not in python ... that is the notation in some other languages though
good catch ... (I assume he is using py2 which is why it worked)
In Python 3, you need to call list(...) too, since this would return a generator. :)
depending on if a generator will work for his needs ... a generator is often perferred (maybe I should have used xrange) .. but its moot since I am fairly confident he is using 2x
0

I guess you mean:

list1 = range(a)
list2 = range(b)

And why these ";" this is python, not javascript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.