Turn the following code into a function. It finds the name in a string and prints it. Pass in the string and return the
Posted: Fri May 20, 2022 5:15 pm
Turn the following code into a function. It finds the name in a
string and prints it. Pass in the string and return the name if it
is found and “Unknown” if not. Be sure to call the function to test
it. Test it both when the name is there and when it isn’t.
code:
namePart = "name: Anu Gao"
posName = namePart.find("name:")
if (posName > -1):
name = namePart[posName+6:len(namePart)]
else:
name = "Unknown"
print(name)
Save & Run Load History i namePart = "name: Anu Gao" 2 posName = namePart.find("name:") 3 if (posName > -1): 4 name = namePart [posName+6:len(namePart)] 5 else: 6 name = "Unknown" 7 print(name) 8
string and prints it. Pass in the string and return the name if it
is found and “Unknown” if not. Be sure to call the function to test
it. Test it both when the name is there and when it isn’t.
code:
namePart = "name: Anu Gao"
posName = namePart.find("name:")
if (posName > -1):
name = namePart[posName+6:len(namePart)]
else:
name = "Unknown"
print(name)
Save & Run Load History i namePart = "name: Anu Gao" 2 posName = namePart.find("name:") 3 if (posName > -1): 4 name = namePart [posName+6:len(namePart)] 5 else: 6 name = "Unknown" 7 print(name) 8