I trying to learn Python but here i lost the connection:
x = 0
y = 0
while x < 5:
for t in "Hello":
y = y + 1
print("x " + str(x) + " y " + str(y))
x = x + 1
What is the connection between for t in "Hello": and str(y) how the programs get the lenght of the word hello ??
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
for t in 'Hello': means that it takes every letter of the string 'Hello'.
so what it does is : for every letter in the string it adds 1 to y.
i got it so everything what is under the for loop is been executed as well ( y = y + 1)
'for t in "Hello":' iterates over the string "Hello", putting each letter into the variable t. All this code does with that information though is increment the value in y (which starts at 0), such that after that for loop finishes executing, y is equal to the number of letters in "Hello". It is a little bit confusing because 'for t in "Hello":' seems to suggest that you want to do something with each letter in "Hello". Python has a built in len() function that can give you the length of a string, so if all you want is for y to equal the length of the string, y = len("Hello") works just fine. If you want do something as many times as there are letters in hello, its a bit more verbose but "for i in range(0,len("Hello")):' seems to better describe what you are trying to do.