-1

I have a next code:

a = int(raw_input())
b = int(raw_input())
c = int(raw_input())

mylist = []

for i in range(a, c):
    mylist.append(i)

print mylist

But by this way I am getting just only 2 elements in list. If I use range(a, d) I'll get an error NameError: name 'd' is not defined

I need add value of a, b, and c into mylist with loop for.

xijhoy
  • 167
  • 2
  • 5
  • 10
  • Python ranges start from 0, so `range(a,c)` does not take into account c. You should rather give `range(3)` – Srivatsan Aug 26 '14 at 08:26
  • 2
    What exactly do you expect this code to do? – sloth Aug 26 '14 at 08:26
  • @sloth: I believe the OP wants to print a,b and c using the `for` loop. But since he is using `range(a,c)`, his code does not print `c` – Srivatsan Aug 26 '14 at 08:28
  • @ThePredator `range(a, c)` when both arguments are `int` will create a list from `a` to `c-1` so `c` will take to account, like sloth said it's not clear what the OP wants. – Kobi K Aug 26 '14 at 08:32
  • 3
    Why did you try `range(a,d)`? I can't see anything called `d`d in your question - maybe that's why python says `NameError: name 'd' is not defined` – doctorlove Aug 26 '14 at 08:46
  • Did you by any chance mean to append a, b and c to mylist? Then you'd do something like for i in [a,b,c]: – Prathik Rajendran M Aug 26 '14 at 08:59
  • @PrathikRajendranM you are exactly right. What about variable d. I thought that range (a, d) don't depend from existing variables and it is ordinary sequence like range (1, 10) just with letters. – xijhoy Aug 26 '14 at 09:11
  • range(1, 10) indicate a range of integers from 1 to 10 when you pass range(a, d) it doesn't mean a range of characters from a to d but it means the range between the value stored in variable a to value stored in variable d, since variable d doesn't exist it is throwing an error. – Prathik Rajendran M Aug 26 '14 at 09:17
  • here is the answer to what you are looking for http://stackoverflow.com/questions/7001144/range-over-character-in-python – Prathik Rajendran M Aug 26 '14 at 09:19
  • @PrathikRajendranM thank a lot! It is really necessary to understanding! And that thing I misunderstood. – xijhoy Aug 26 '14 at 09:24

3 Answers3

5

I think you may want this:

a = int(raw_input())
b = int(raw_input())
c = int(raw_input())

mylist = []

for i in [a, b, c]:
    mylist.append(i)

print mylist
Stephen Lin
  • 4,852
  • 1
  • 13
  • 26
  • 1
    I do not understand how you came to the conclusion that this is what the OP wants. – Kobi K Aug 26 '14 at 08:34
  • 2
    @KobiK I think it's pretty clear that this is what OP had in mind, otherwise he would probably not try `range(a, d)`. +1 – tobias_k Aug 26 '14 at 09:04
  • @tobias_k You can also say that he wants the entire range from `a -> d`, and not just `a,b,c,` as in the above answer. I appreciate the effort for trying to help, But to IMHO i think it is best to first get a clear question from the OP and then provide an answer :) . – Kobi K Aug 26 '14 at 09:09
  • @KobiK Yeah, OP has got -3 already. This question is unclear indeed. I think waiting for OP's response is the best choice. And my answer is , I think the best for this question. – Stephen Lin Aug 26 '14 at 09:13
  • @m170897017 thanks a lot. It is what I looking for. Sorry for unclear question. – xijhoy Aug 26 '14 at 09:22
  • 1
    `mylist = [a, b, c]` would be faster. – Matthias Aug 26 '14 at 10:04
1

Why to do such thing?

for i in range(a, c):
    mylist.append(i)

instead, do:

mylist = range(a, c)
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
0

Now your comments have clarified the question.

To change between characters and numbers use ord and chr. Notice the difference between the name you give a variable, and the value it takes on.

e.g.

def letters(start, end):
  begin = ord(start)
  end = ord(end)
  for item in range(begin, end):
    yield chr(item)


>>> for item in letters('a', 'd'):
...   print item
...
a
b
c
doctorlove
  • 18,872
  • 2
  • 46
  • 62