-1

"Your task is to write a function that adds a prefix or suffix to a person's name. The name of your function should match exactly as shown below, including cases (all lowercase")

def fix_names(name, position, added_phrase):
    name = ''
    added_phrase = ''
    position = "prefix" or "suffix"
    if (position == "prefix"):
        prefix = added_phrase
        print (added_phrase+name)

    elif (position == "suffix"):
        suffix = added_phrase
        print(name+added_phrase)

    return(fix_names)

fix_names("John", "prefix", "Dr.")

This is my code, but when I run it, I don't receive any output. Any tips/suggestions to make it work? Thank you

jdavis29
  • 41
  • 5

4 Answers4

1

The name, added_phrase, etc. are coming into the function as variables. But, you are setting them to blanks, which is why you are not seeing any output for print. Also, the return should have the newname you want to send back to the main function, not fix names. Updated code here...

def fix_names(name, position, added_phrase):
#    name = ''
#    added_phrase = ''
#    position = "prefix" or "suffix"
    if (position == "prefix"):
        prefix = added_phrase
        print (added_phrase+' '+name)
        newname=added_phrase+' '+name

    elif (position == "suffix"):
        suffix = added_phrase
        print(name+' '+added_phrase)
        newname=name+' '+added_phrase
        
    return(newname)

variable = fix_names("John", "prefix", "Dr.")
## Do something else with the new name....
Redox
  • 9,321
  • 5
  • 9
  • 26
  • Ohhhh okay I see thank you very much. One issue I'm experiencing now thought is when I receive the output, there aren't any spaces between the name and the prefix/suffix. Like the output yields Dr.John instead of Dr. John. Do you know how I would fix that? – jdavis29 Oct 06 '22 at 20:23
  • Just add `' '+` in between. Updated answer with the spaces.. – Redox Oct 06 '22 at 20:25
0

Argument passed were overwritten with blank string in your function, and some variables are not used or redundant, also your return value should be your print function or does not need a return value if you're printing directly from function.

def fix_names(name, position, added_phrase):
    if position == "prefix":
        return f'{added_phrase} {name}'
    elif position == "suffix":
        return f'{name} {added_phrase}'

print(fix_names("John", "suffix", "Dr."))

Or:

def fix_names(name, position, added_phrase):
    if position == "prefix":
        print(f'{added_phrase} {name}')
    elif position == "suffix":
        print(f'{name} {added_phrase}')

fix_names("John", "suffix", "Dr.")
Arifa Chan
  • 947
  • 2
  • 6
  • 23
0

In your function definition, you are overwriting the value of the parameters

name = ''
added_phrase = ''
position = "prefix" or "suffix"

Here is the working code

def fix_names(name, position, added_phrase):
    fix_name = added_phrase + name if position == "prefix" else name + added_phrase
    return fix_name

op = fix_names("John", "prefix", "Dr.")
print(op)
Ravi
  • 198
  • 1
  • 1
  • 8
-1

This might not answer the question exactly, since I don't understand it fully, but it could have something to do with you setting the "name" and "added_phrase" arguments to blank at the start of the function, defeating the purpose of having them. Also, make sure not to have a space between functions and brackets, as this is a syntax error.

EDIT: Also, as said in the comments, "position = 'prefix' or 'suffix'" isn't a valid line, and isn't needed. You are already defining position with the argument, so you wouldn't need this even if it worked.

Also, try to make your questions clearer on what you're trying to do so people can help you efficiently, and try to do research before asking a question on here.

Example of fixed code:

def fix_names(name, position, added_phrase):
    if (position == "prefix"):
        prefix = added_phrase
        print(added_phrase+name)

    elif (position == "suffix"):
        suffix = added_phrase
        print(name+added_phrase)

fix_names("John", "prefix", "Dr.")
OnMyTerms
  • 13
  • 4
  • Also, what program are you using to run python? The typical IDLE launcher and virtually all other launchers don't let you run a program if it has syntax errors. – OnMyTerms Oct 06 '22 at 20:07