Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with list
#1
input("give number a")
a= int(input())
input("give number b")
b=int(input())
luck = list()
for i in range(0,b):
   luck[i] = a**b
print (luck)
i am getting this mistake:
Error:
File "/home/main.py", line 7, in <module> luck[i] = a**b IndexError: list assignment index out of range
the exercise is given 2 numbers a and b create a list with [a^0,a^1,a^2,...,a^b]

but it is not working
Reply
#2
You created empty list. Therefore index is out of range:

>>> luck = list()
>>> len(luck)
0
>>> luck[0]
/..../
IndexError: list index out of range
You should use append.

You also ask two times for input for both a and b.

One way of writing this code:

>>> a = int(input("Give number a: "))
>>> b = int(input("Give number b: "))
>>> luck = list()
for i in range(b):
...    luck.append(a**b)
Of course, if task is [a^0,a^1,a^2,...,a^b] then one should append (a**i)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(Jan-30-2019, 04:39 PM)perfringo Wrote: You created empty list. Therefore index is out of range:

>>> luck = list()
>>> len(luck)
0
>>> luck[0]
/..../
IndexError: list index out of range
You should use append.

You also ask two times for input for both a and b.

One way of writing this code:

>>> a = int(input("Give number a: "))
>>> b = int(input("Give number b: "))
>>> luck = list()
for i in range(b):
...    luck.append(a**b)
Of course, if task is [a^0,a^1,a^2,...,a^b] then one should append (a**i)

thanks but it's not working either
what i want is to print the follow :
example a=2 b=4
should print [2^0,2^1,2^2,2^3,2^4] which is [1,2,4,8,16]
Reply
#4
(Jan-30-2019, 04:47 PM)sonedap Wrote:
(Jan-30-2019, 04:39 PM)perfringo Wrote: Of course, if task is [a^0,a^1,a^2,...,a^b] then one should append (a**i)

thanks but it's not working either
what i want is to print the follow :
example a=2 b=4
should print [2^0,2^1,2^2,2^3,2^4] which is [1,2,4,8,16]

You should read carefully. I just refactored your code and made observation that (a**i) should be used. Also, the range should be range(b+1) if b must be included as well.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Jan-30-2019, 04:56 PM)perfringo Wrote: You should read carefully. I just refactored your code and made observation that (a**i) should be used. Also, the range should be range(b+1) if b must be included as well.

sorry!My mistake
well,i run it but the result is this one:
[1, 4]
[1, 4, 16]
[1, 4, 16, 64]
[1, 4, 16, 64, 256]
[1, 4, 16, 64, 256, 1024]
[1, 4, 16, 64, 256, 1024, 4096]

i want only the last line though.is there a way to make that happen?
Reply
#6
Show us your current code. I'm guessing you need to unindent the print to get it out of the for loop, but I can't tell for sure without the latest code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Jan-30-2019, 05:37 PM)ichabod801 Wrote: Show us your current code. I'm guessing you need to unindent the print to get it out of the for loop, but I can't tell for sure without the latest code.

a = int(input("Give number a: "))
b = int(input("Give number b: "))
luck = list()
for i in range(b+1):
    luck.append(a**i)
    print (luck)
should i use "break" after luck.append(a**i) ??
Reply
#8
No, you should unindent print, as I said. Where it is now, it is part of the for loop code. If you unindent it to be even with the for statement, it will only execute once after the loop is done.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(Jan-30-2019, 05:50 PM)sonedap Wrote: should i use "break" after luck.append(a**i) ??

Try it and see.
Did you get the result you wanted?
Why or why not?

Programming puts you in the shoes of a scientist, creating hypotheses and testing them repeatedly. You should get in the habit of trying things out.
Reply
#10
(Jan-30-2019, 09:00 PM)nilamo Wrote:
(Jan-30-2019, 05:50 PM)sonedap Wrote: should i use "break" after luck.append(a**i) ??

Try it and see.
Did you get the result you wanted?
Why or why not?

Programming puts you in the shoes of a scientist, creating hypotheses and testing them repeatedly. You should get in the habit of trying things out.
i agree with you!
it's just that i am new to Python and i am trying to understand how Python is working.
For exaple i have one more exercise in which i have queries but i dont want to ask here yet.i am tryinh to solve it alone.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020