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.
8 ответов
+ 4
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
+ 4
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.
+ 3
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
+ 2
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.
+ 2
Igor Matić In Python, parentheses are overloaded to fill several roles.
1 - right after a function name to call the function with provided arguments
2 - part of math equation to change order of precedence
3 - standalone to define a Python collection called a tuple, which is an unchangeable ordered list
text = ("#", s)
In this case, parentheses are interpreted as usage #3 to create a tuple with "#" as first element, and whatever is stored in variable s as 2nd element.
Assuming you earlier assigned user input to s with
s = input()
then concatenating # with that should be
text = "#" + s
+ 1
Thank you both. I was simply overthinking it. Just solved the task.
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.