Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

ilianpetrov05

Hi there, 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 ??

  • one year ago
  • one year ago

  • This Question is Open
  1. MicroBot
    Best Response
    You've already chosen the best response.
    Medals 0

    for t in 'Hello': means that it takes every letter of the string 'Hello'. t='H' t='e' t='l' etc so what it does is : for every letter in the string it adds 1 to y.

    • one year ago
  2. ilianpetrov05
    Best Response
    You've already chosen the best response.
    Medals 0

    i got it so everything what is under the for loop is been executed as well ( y = y + 1)

    • one year ago
  3. myndless
    Best Response
    You've already chosen the best response.
    Medals 3

    '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.

    • one year ago
  4. MicroBot
    Best Response
    You've already chosen the best response.
    Medals 0

    @myndless great answer

    • one year ago
  5. ilianpetrov05
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks for the answers !

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.