简体   繁体   中英

Learn Python 3 the Hard Way ex38

This is my first question on the forum and it is related to Zed Shaw's LPTHW ex38. I want to thank you before hand for any and all assistance I get with my inquiry beforehand.The code for the example I am having an issue with is right here.

ten_things = "Apples Oranges Crows Telephone Light Sugar"

stuff = ten_things.split(" ")

more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]

while len(stuff) != 10:

        next_one = more_stuff.pop()

        print("Adding: ", next_one)

        stuff.append(next_one)

        print(f"There are {len(stuff)} items now.")

My first question with this script is with len(stuff)in the while loop. This variable contains a string that was split via white space and at the beginning of the loop, should have the value 42 even though it was split. Because elements are being removed via pop from more_stuff and appended to the end of stuff, how does this change the len(stuff) value from character to element in list(42 characters to 10 elements)?

len(stuff) 42

Prior to, and after the split(), len will have a value of 42. after the append() the value will change to function as a list maybe. Am I wrong to say this?

My second question is how would you substitute a for loop in place of the while in this example?

Thanks again!

First question:

Your code has a split , that's why it is only ten, if you didn't do that:

ten_things = "Apples Oranges Crows Telephone Light Sugar"
stuff = ten_things
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
while len(stuff) != 10:
        next_one = more_stuff.pop()
        print("Adding: ", next_one)
        stuff.append(next_one)
        print(f"There are {len(stuff)} items now.")

You get an error:

Adding:  Boy
Traceback (most recent call last):
  File "C:\Users\rep\Desktop\code\so.py", line 8800, in <module>
    stuff.append(next_one)
AttributeError: 'str' object has no attribute 'append'

So that's why.

Second question:

You can use a for loop this way:

ten_things = "Apples Oranges Crows Telephone Light Sugar"
stuff = ten_things.split()
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
for i in more_stuff[::-1]:
    if len(stuff) == 10:
        break
    else:
        print("Adding: ", i)
        stuff.append(i)
        print(f"There are {len(stuff)} items now.")

Iterate through the reverse of more_stuff and do a if statement to break if there is 10 elements, otherwise the same.

You are confusing characters with elements.

mylist = ["element 1", "element2", "element_3"]
print(len(mylist)) #This will give you 3 on screen
                   #Because you are counting the elements
                   #not the words or the characters
                   #an element ends when a comma arrives.

And for the other question,

...how would you substitute a for loop in place of the while in this example?

I do the same code than U9-Forward

First, stuff is an array and it should not have the value 42.

The split method operates over a string and it returns an array split by a delimiter.

split Docs: https://docs.python.org/3/library/stdtypes.html#str.split

So the value of stuff is:

['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar']

The condition len(stuff) != 10 starts with 6 != 10 so the code continues appending elements from more_stuff until the size of the stuff array is 10.

Your confusion is on split method.

For your second question, you can substitute for a for loop as follows:

for word in more_stuff: 
    if len(stuff) == 10:
       break
    stuff.append(word)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM