0

Why does this keep giving me 28 as the output?

def rep(): x=input() y=str(x) print (y) def equ(e): a=str(rep) b=len(a) print (b) equ(rep)

17th Nov 2016, 11:13 PM
Kemuel Samuels
Kemuel Samuels - avatar
5 ответов
+ 2
You probably want this. def rep(): x = input() return x def equ(): a = rep() b = len(a) print(b) Why 28? You did "str(rep)" instead of "str(rep())", so it actually converted the entire rep function to a string, which happens to be 28 characters long. Funny bug!
17th Nov 2016, 11:31 PM
Schindlabua
Schindlabua - avatar
+ 2
Here's what your code does: It prints the length of a string obtained by converting a function into a string. Now, if that seems right to you then great! Maybe this is what you wanted your code to do: Read a string typed by the user and get its length. If that is the case, here's what your code should have been: def rep(): x = input() y = str(x) # This is pointless, x is already a string return x # Same thing as returning y. Different from printing def equ(e): a = str(e) b = len(a) return b equ(rep()) # All parentheses are important here Now, if you want your code to be better then it should look like this: len(input()) # This does the exact same thing
17th Nov 2016, 11:36 PM
Arthur Busser
Arthur Busser - avatar
0
This is just a part of a code; I do want it to give the length of what is input, but there are several layers to it. I tried just using len (input ()) but I get an EOF error from it. I will load the entire code as I have it now.... in all its bugginess
17th Nov 2016, 11:42 PM
Kemuel Samuels
Kemuel Samuels - avatar
0
I want the input to be it's own entity so that I can use it later; to get it's length, reverse it, add to it, etc....
17th Nov 2016, 11:44 PM
Kemuel Samuels
Kemuel Samuels - avatar
0
def rep(e): x=input() y=str.x print (y) inp=input("Give me a name: ") x=len(inp) if x<10: print ('Your name has less \n than 10 letters') else: print ('Your name has more \n than 10 letters') This is the start of the code, I posted it as it's own question.
17th Nov 2016, 11:48 PM
Kemuel Samuels
Kemuel Samuels - avatar