Home > front end >  Iterating with multiple variables
Iterating with multiple variables

Time:11-10

I am taking a course from Georgia Tech and I have spent all my evening trying to figure this out and I havent been able to do so. My task is as follows:

Write a function called my_TAs. The function should take as input three strings: first_TA, second_TA, and third_TA. It should return as output the string, "[first_TA], [second_TA],#and [third_TA] are awesome!", with the values replacing the variable names.

For example, my_TAs("Sridevi", "Lucy", "Xu") would return the string "Sridevi, Lucy, and Xu are awesome!".

Hint: Notice that because you're returning a string instead of printing a string, you can't use the print() statement -- you'll have to create the string yourself, then return it.

My function returns "Joshua are awesome" instead of all three variables names. I tried this

result = str(first_TA), str(second_TA), str(third_TA)   "are awesome!"

but didn't work.

def my_TAs(first_TA, second_TA, third_TA):
    result = str(first_TA)   " are Awesome!"
    return result


first_TA = "Joshua"
second_TA = "Jackie"
third_TA = "Marguerite"


test_first_TA = "Joshua"
test_second_TA = "Jackie"
test_third_TA = "Marguerite"
print(my_TAs(test_first_TA, test_second_TA, test_third_TA))

CodePudding user response:

You can use f-Strings to accomplish this:

def my_TAs(first_TA, second_TA, third_TA):
    return f"{first_TA}, {second_TA}, and {third_TA} are awesome!"
    
test_first_TA = "Joshua"
test_second_TA = "Jackie"
test_third_TA = "Marguerite"
print(my_TAs(test_first_TA, test_second_TA, test_third_TA))

Output:

Joshua, Jackie, and Marguerite are awesome!

CodePudding user response:

Use instead of ,

def my_TAs(first_TA, second_TA, third_TA):
        result = str(first_TA)   ", "   str(second_TA)    ", and "   str(third_TA) 
  " are Awesome!"
        return result
    
    
    first_TA = "Joshua"
    second_TA = "Jackie"
    third_TA = "Marguerite"
    
    
    test_first_TA = "Joshua"
    test_second_TA = "Jackie"
    test_third_TA = "Marguerite"
    print(my_TAs(test_first_TA, test_second_TA, test_third_TA))

  • Related