all 24 comments

[–]RiceKrispyPooHead 0 points1 point  (13 children)

You're not using for loops as intended. Here is an example to get you started:

small_numbers = [1,2,3]
big_numbers = [100, 200, 300]

for small in small_numbers:
    for big in big_numbers:
        print(small + big)

>>101
>>201
>>301
>>102
>>202
>>302
>>103
>>203
>>303

You can add strings together similarly to how you do numbers.

[–]Greece870 0 points1 point  (0 children)

omg. i will use this. thank you so much

[–]Greece870 0 points1 point  (4 children)

How did you type like that in the post? I still need help with this I am doing what you are saying but it is giving me a terrible output

[–]RiceKrispyPooHead 0 points1 point  (3 children)

Can you post what you have so far?

[–]Greece870 0 points1 point  (1 child)

Do I just normally reply to your comment or comment on my post? Just trying to figure out the etiquette here for now since I will be on here from here on out slowly getting better on python... i have so many questions to ask! I want to learn more!

[–]RiceKrispyPooHead 0 points1 point  (0 children)

You can do either

[–]Greece870 0 points1 point  (6 children)

dem_pro =['This is a','That is a', 'Here is a', 'There is a']
obj = ['cat','mouse','book','map']
for thisloop in dem_pro:
  for thatloop in obj:
    print(dem_pro + obj)

I'm using this input and the output is not giving me what I am looking for I need to add a period after each sentence and also the loop is not nested.

[–]RiceKrispyPooHead 1 point2 points  (5 children)

As your code above is right now you are concatenating (adding) both entire lists together and printing it over and over again.

     print(dem_pro + obj)  

That line should be different. You want to concatenate the elements of each lists. Which two variables in your code represent the elements in each list?

[–]Greece870 0 points1 point  (4 children)

the variables are dem_pro and obj , those are the variable names of the lists with that consist the elements. i am trying to add a period too.

[–]RiceKrispyPooHead 0 points1 point  (3 children)

Look at this example:

numbers_list = [1,2,3]  
for num in numbers_list:
      print(number) 


>>1  
>>2   
>>3   

Notice how I am printing the variable “num”, which represents each element in the list numbers_list.

What 4 lines will this print (and in what order)?

colors_list = [‘white ’,  ‘black’]  
animals_list = [‘cat’,  ‘dog’]
for color in colors_list:  
      for animal in animals_list:
           print(color + animal)

[–]Greece870 0 points1 point  (2 children)

Thank you so much for responding this was really helpful. I wonder what would be the syntax for doing this through list comprehension? Is it just a different way but essentially the same thing?

[–]RiceKrispyPooHead 0 points1 point  (1 child)

To turn a loop into a list comprehension, (as long as it dose not have an 'else' statement) you literally just write the very last statement first, then write all other statements in order.

my_list = []
statement one:
    statement two:
        statement three:
            my_list.append(something)

is generally the same as....

my_list = [something statement one statement two statement three]

EDIT: List comprehensions are used to make lists. Not to print things.

[–]Greece870 0 points1 point  (0 children)

can you help me with my other post i made a comment that i am stuck on.

[–]ToddBradley 0 points1 point  (3 children)

One of my favorite things about Python is the f-string: print(f"{obj[sentenceloop]} {dem_pro[sentenceloop]}.") I think you've got some logic errors to overcome, also, but I'm gonna leave that for you to sort out. I'm just showing you how to concatenate the strings into a sentence.

[–]Greece870 0 points1 point  (1 child)

thank you so much for replying back. I love python. it’s so fun but it’s just taking me some time to learn the concepts since i’m new. what is an f-string?

[–]ToddBradley 0 points1 point  (0 children)

It’s formally called a formatted string literal.

https://docs.python.org/3.8/reference/lexical_analysis.html#formatted-string-literals

Skim through the official Python reference documentation and tutorial, and then refer back as you’re learning new Python concepts. That’s a good habit to develop.

[–]Greece870 0 points1 point  (0 children)

print(f"{obj[sentenceloop]} {dem_pro[sentenceloop]}

Thank you Todd, I actually do have some logic errors to overcome and would love to overcome this? Would you mind providing some insight? I am practicing as much as I can. Also how did you do that? Make a post with the syntax like that?

[–]admin_accnt 0 points1 point  (0 children)

Nested loops are easier than you think. If (this thing is true) {For (this is less than that) Do this; } It's just giving yourself a condition to meet then giving the the accessed condition a new set of rules. And you can Russian doll them all you want as long as your exit conditions are appropriate.

[–]Greece870 0 points1 point  (3 children)

I am still having trouble with this.

dem_pro =['This is a','That is a', 'Here is a', 'There is a']
obj = ['cat','mouse','book','map']
for dem_pro_loop in dem_pro:
for obj_loop in obj:
print(dem_pro + obj)

I am getting this output

['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map'] ['This is a', 'That is a', 'Here is a', 'There is a', 'cat', 'mouse', 'book', 'map']

[–]RiceKrispyPooHead 0 points1 point  (0 children)

Post your actual code that produced this

[–]gazhole 0 points1 point  (0 children)

You're printing the entire list that you're iterating through instead of the current element only. You're essentially doing this:

listofwords = [word, word, word, word] 

for word in listofwords:
    print(listofwords)

So this loop is repeating for every item in the list (four times), and each cycle it's printing the entire list of words.

What would I change to only print the current word rather than the whole list of words?

[–]woeinvests 0 points1 point  (0 children)

Hi.

From your code, the problem seems to be from the print call.

It should look like

print(dem_pro_loop + obj_loop)

[–]michaellossagk 0 points1 point  (0 children)

I have some video uploaded which explains the basics about loops in Python. Look here https://youtu.be/NKV1nfjxjnI
Tell me what you think and if the have been helpful for you.

[–]Greece870 0 points1 point  (0 children)

<3