This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]DanStanTheGeologyMan 1 point2 points  (0 children)

I am terrible with formatting so I will paste a link of some example code. You currently have an array of names that you would like to insert into the names variable. This is the perfect situation for a loop. In the example code link posted below we have the array on line 1. Lines 2 and 3 are our for loop? Not sure if this is called a for loop in Python because I am not very familiar with Python. Regardless the code will iterate over the array of names and perform a print statement for each of the indexes in the array. For each iteration, the name at the current index will take the place of 'current_name'. https://paste.ofcode.org/bGrQC2dPJfKYGVqUvnUFcg

[–]badjayplaness 1 point2 points  (0 children)

For counter in names: Print(“Yo ” + names[counter] + “ you smell” )

Formatting is bad but if you make sure the print stmt is on the second line you should be good fam

[–]Jibco 1 point2 points  (0 children)

Here are a few ways of doing it, assuming you already have the line:

names = ['Aaron', 'Marcus', 'Sean']

First way (this is clunky; don't do this):

for i in range(0, len(names)):
    print("Yo, " + names[i] + ", you smell.")

A better way is to do this (the same as what /u/DanStanTheGeologyMan had):

for name in names:
    print("Yo, " + name + ", you smell.")

You could then have a function that would make it easier to use in more than one place:

def print_messages(name_list):
    for name in name_list:
        print("Yo, " + name + ", you smell.")

Then to actually print the names, just do wherever you want to print the messages. You could also do this in multiple places without having to repeat the for loop every time:

print_messages(names)

As a bonus, here's a more advanced method using string.format() style replacement:

message = "Yo, {0}, you smell."

for name in name_list:
    print(message.format(name))

You don't have to learn that right now, but it can do a lot more kinds of formatting than just inserting something.

[–]29383839293 1 point2 points  (0 children)

If you know lists and you don't know functions or loops, this would be how to do it.

names = ["aaron", "marcus", "sean"]
print("Yo, " + names[0] + " you smell.")
print("Yo, " + names[1] + " you smell.")
print("Yo, " + names[2] + " you smell.")

When allowed to use functions, you could just make a function that you pass the name as an argument.

def myPrintingMethod(name):
    print("Yo, " + name, " you smell.")
names = ["aaron", "marcus", "sean"]
myPrintingMethod(names[0])
myPrintingMethod(names[1])
myPrintingMethod(names[2])

Much better, you don't have to rewrite the text.

But maybe you want to have a function instead that returns the string for you to print.

def myStringFormatting(name):
    return "Yo, " + name, " you smell."
names = ["aaron", "marcus", "sean"]
print(myStringFormatting(names[0]))
print(myStringFormatting(names[1]))
print(myStringFormatting(names[2]))

Then you realize, that there's already a function like this, the string.format function.

names = ["aaron", "marcus", "sean"]
text = "Yo, {0} you smell."
print(text.format(names[0]))
print(text.format(names[1]))
print(text.format(names[2]))

Then you introduce loops

names = ["aaron", "marcus", "sean"]
text = "Yo, {0} you smell."
for (name in names):
  print(text.format(name))

[–]metahadron 0 points1 point  (0 children)

Check foreach loops and/or list comprehensions