0
Why am i getting either an attribute error, or no output at all?
https://www.sololearn.com/en/compiler-playground/cUc9ZJzhIOX4 Attribute error happens when there's a coma in the line 6, it reports that i'm trying to work with a tuple. How did input become tuple when it should be a string? And if i replace the coma in the same line with a +, being the only change, the code gives no output.
5 Antworten
+ 3
I think you've misunderstood what you're meant to do and what your function does.
Let's look at the function.
def hashtagGen(text):
- is the name of the function which takes "text" as an argument.
text = ("#", s)
- tries to change the argument "text" in an invalid way. The variable "s" hasn't been defined before.
text = text.replace(" ", "")
- replaces the "text" with a new "text" removing the spaces.
return
- doesn't return anything.
I'll do an example, hopefully it helps.
def addFive(num):
newNum = num + 5
return newNum
+ 3
text = ("#", s)
creates a tuple with the values "#" and s. It doesn't concatenate anything. Also, it overwrites the text that you pass to your function.
+ 1
Your code:
def hashtagGen(text):
#your code goes here
text = ("#", s)
text = text.replace(" ", "")
return
s is not defined in the context of your function.
it is unclear to me what text = ("#", s) is supposed to do. text is the value that you pass to the function – it is a string.
.replace() is a string method. It replace substrings.
The function does not return any value. It should return the modified text, I suppose.
0
@Lisa, the line text = ("#", s) is supposed to concatenate # with user input that's assigned earlier to s. I don't understand how does the input becomes a tuple.
0
Igor Matić
the comma concatenates only in the print() function. It creates a tuple otherwise. To concatenate, you need to use +
https://sololearn.com/compiler-playground/clHYcxQPwAPj/?ref=app