[Tutor] How to manipulate a user's input twice in one statement
Alan Gauld
alan.gauld at yahoo.co.uk
Wed May 17 16:29:48 EDT 2023
On 17/05/2023 15:24, Goran Ikac wrote:
> Aha! So, "removesuffix" can be used as a method applied to an object (the
> instance of <class 'str'>), but not as a function with a string as the
> first argument. The first argument must be "self".
Correct. self is the string instance in the method definition but when
you use it the string instance is the prefix object. The difference
between a function and a method is that Pyhthon automatically converts
object.method(args)
to
method(object, args)
behind the scenes. (It does a bit more than that but from
a users perspective it's close enough)
>>>> input().removesuffix(input()[1:])
>
> ... and it didn't work as I expected.
I guess that's what I'm confused about.
input() is a function like any other. It returns a string obtained from
stdin(usually the user but it could be from another program or file)
And you call input() twice here, so lets unpick how Python sees this.
First it sees the first input and recognizes that it must return
a string, so it calls it and grabs the string returned. It then
calls the removesuffix() method of that string object.
But removesuffix() takes a string as an argument and to get that
string Python must call the second input() function invocation.
That returns another string object which Python then passes
to removesuffix.
After removing the suffix Python gets handed yet another
string object as a result which it then displays.
So Python converts your code into:
print( first_string.removesuffix(second_string) )
I'm not sure what you expected it to do?
>>>> result = 3 * (2 + int(input('Please, type a number: '))
This is the same but you only call input() once.
But Python processes it in the same way.
It sees result = 3 * (2 + int(some_string))
and some_string is obtained by calling input()
The only difference is that you had two calls to input() in
the first case but only one in the second example.
There is nothing magic about input() that would make
Python remember its previous result any more than it
remembers the earlier result from, say pow()
So if you type:
hyp = pow(pow(x,2) + pow(y,2), 0.5)
pow() is called three times each completely independent
of the others. It's the same with input(), it is just a
function like any other.
> ... worked, and I guessed if I can make the method work like a function or
> an operator would.
> Obviously, I need to learn better about classes and methods.
I'm not sure that's the problem here. Apart from the fact
that removesuffix() is a method of a string rather than a
stand-alone function it doesn't change how it interacts
with input()
In fact you could create a function that would do the
same job and it would have the same effect:
def remove_suffix(aString, aSuffix):
return aString.removesuffix(aSuffix)
now you could try calling it with input():
remove_suffix(input(),input())
You would still get the same double call to input()
If you wanted to use the same result(and slice it you
could use the walrus operator:
remove_suffix(s := input(), s[1:])
but that still requires a variable it just keeps it
inside the line(but it is still available afterwards too!).
I hope that makes sense.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list