| | |
Help with adding to an empty list
![]() |
0
#2 Oct 19th, 2009
To do it the way you're trying to do it, use this:
Notice I put brackets around
HTH
python Syntax (Toggle Plain Text)
>>> num = 0 >>> mylist = [] >>> >>> while num < 10: ... num += 1 ... mylist += [num] ... >>> for item in mylist: ... print item ... 1 2 3 4 5 6 7 8 9 10 >>>
num since you have to add a list to a list when using the + operator. Other methods would be to use mylist.append(num) or mylist.insert(index, num) . I also changed your x = x + y statements to x += y , which is identical.HTH
Last edited by jlm699; Oct 19th, 2009 at 6:56 pm.
0
#3 Oct 19th, 2009
Some corrections:
python Syntax (Toggle Plain Text)
num = 0 mylist = [] while num < 10: num = num + 1 mylist.append(num) print(mylist) for item in mylist: print(item)
Should you find Irony, you can keep her!
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#5 Oct 19th, 2009
I'm not getting it to print anything! I'll post the exact code I am working with... I'm trying to convert from deci to binary and print the binary number using the list method.
remain = 0
quo = 1
mynum = []
deci = int(raw_input("Please enter a decimal integer:"))
while quo != 0:
quo = deci / 2
remain = deci / 2
mynum.append(remain)
print(mylist)
for item in mynum:
print item
remain = 0
quo = 1
mynum = []
deci = int(raw_input("Please enter a decimal integer:"))
while quo != 0:
quo = deci / 2
remain = deci / 2
mynum.append(remain)
print(mylist)
for item in mynum:
print item
0
#6 Oct 19th, 2009
"I'm not getting it to print anything! I'll post the exact code I am working with... I'm trying to convert from deci to binary and print the binary number using the list method."
...
so you're trying to convert a number from decimal to binary??
here's a hint how to do that
number = 14
14 / 2 = 7, remainder = 0
7 / 2 = 3, remainder = 1
3 / 2 = 1, remainder = 1
1 / 2 = 0, remainder = 1
therefore 14 in binary is 1110
you can use l.insert( 0, num ) to deal with the reverse order, where l is a list
hope that helps!
...
so you're trying to convert a number from decimal to binary??
here's a hint how to do that

number = 14
14 / 2 = 7, remainder = 0
7 / 2 = 3, remainder = 1
3 / 2 = 1, remainder = 1
1 / 2 = 0, remainder = 1
therefore 14 in binary is 1110
you can use l.insert( 0, num ) to deal with the reverse order, where l is a list
hope that helps!
Last edited by masterofpuppets; Oct 19th, 2009 at 9:05 pm.
![]() |
Similar Threads
- Nested Data Source Binding error only when List is empty (C#)
- Linked Lists: Find length and check for empty list (C++)
- problem with template in c++ (C++)
- Recursive procedure (to display a linked list) (Pascal and Delphi)
- Scheme empty list help (Legacy and Other Languages)
- help with this code of link list (C++)
- help with adding items to a linked list. (Java)
- Help w/ dynamic list funtction definitions (C++)
- new member here :) (C)
Other Threads in the Python Forum
- Previous Thread: ??OOP??
- Next Thread: pygtk + py2exe + inno -> icon for shortcut
| Thread Tools | Search this Thread |







