Someone
Someone

Reputation: 35

Printing Results as Vertical List

Student learning Python3 here.

I have an assignment where I am told to write something for this:enter image description here

My current code that I have is:

from read_lines import read_lines

storedstrings = read_lines()
print(storedstrings)

ci = 0
contin = True
while contin:
        if ci > len(storedstrings):
                contin = False
        else:
                storedstrings = storedstrings[0]
                ci = ci + 1
                print(ci, storedstrings)

But when I run it, my result is:

[vbox]~/CSE1010/HW4$ python3 loop.py
abc
def
ghi

['abc', 'def', 'ghi']
1 abc
2 a
[vbox]~/CSE1010/HW4$

I cannot seem to get the second or third index to come out correctly, though the first one is fine.

Would appreciate any help.

Thank you guys for your time!

Upvotes: 0

Views: 451

Answers (1)

vallentin
vallentin

Reputation: 26157

The problem is storedstrings = storedstrings[0]. You're taking the first string, and assigning it to the same name. So next time storedstrings[0] happens, you are getting the first character of the first string. What you instead would want to do is:

string = storedstrings[ci]

Instead of using that while loop, it would be easier to use a for loop along with enumerate(). Then it would be as simple as:

for i, string in enumerate(storedstrings, start=1):
    print(i, string)

Output:

1 abc
2 def
3 ghi

Upvotes: 1

Related Questions