r/inventwithpython • u/shonu-FF • Feb 20 '17
[AUTOMATE] Ch. 4 Lists - Comma Code Exercise
Hello World! first post ever to Reddit!
Just wanted to know how you guys found a solution to the Comma Code exercise in ch. 4 of automate the boring stuff.
I did a quick search for a solution but couldn't find one.
I know the exercise states to make a function but my solution was this...
name = input("Gimme a list value: ")
newName = name[0:-1] + " and " + name[-1]
print(newName)Any (constructive) comments? I'll be mashing away trying to find a solution with a function in the mean time.
2
u/Danseurdeventre Feb 22 '17
Hey! Congrats on your first post. I haven't done automate, but I've done some python, and it looks like the problem is that your variable "name" is a string-object, but you're using a operation that operates on a list-object. The [0:-1] operation is what you use to splice a list, so one of the things you can do is find a way to convert your variable "name" from a string into a list. Or you can find an operation that does a similar splice operation to a string. Does this seem to be in line with the error you're getting?
2
u/Danseurdeventre Feb 22 '17
You can go from a string to a list with n=list(name). But it seems there is more going on here than that. I've been playing with your code, and I keep getting the error "name not defined".
name = raw_input("Gimme a list value: ")
That should take care of that error. But there's now the issue of trying to concatenate two list objects with a string object. They all need to be the same type, I believe. I haven't done this in forever. And it's bedtime, but hopefully that will give you some ideas.
2
u/shonu-FF Feb 25 '17 edited Feb 25 '17
Hey
guysDanseurdeentre, thanks for the insight. But all I really needed to do was make it a function. The code worked when it ran, but my problem was making it a function. It was so simple I felt dumb when I figured my solution. Here it isedited for formatting and the thank you note lol