"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
CodePudding user response:
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....
CodePudding user response:
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)
CodePudding user response:
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.")
CodePudding user response:
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.")