[Tutor] How to manipulate a user's input twice in one statement

Mats Wichmann mats at wichmann.us
Tue May 16 13:27:07 EDT 2023


On 5/16/23 10:35, ThreeBlindQuarks via Tutor wrote:
> 
> I agree with Alan that we may not yet understand what is wanted.
> 
> For now, the request may fit into a class of posts that boil down to wanting the language to be designed differently to do things in a way they would like. Often that is done without a full understanding of what that might entail.
> 
> In this case, again, I am not understanding what is being asked as a fairly simple method is available unless there is some great reason for wanting a one-liner. But Pythos has been augmented to allow many such one-liners.
> 
> Some languages have an inline operator allowing you to set the value of a variable in middle of an expression and reuse it. Python did add the walrus operator ":=" that MAYBE is what the user wants as in:
> 
>>>> x = (y := 10) + y + y**2
>>>> x
> 120
> 
> So it can be as simple as assigning your input using the walrus operator in a sort of inline way to save the input and use it later.
> 
> With a slight change for clarity, the code below works in the example, albeit I have no idea what is wanted:
> 
>>>> input("enter text: ").removesuffix(input("enter suffix: ")[1:])
> enter text: hello.avi
> enter suffix: avi
> 'hello.a'
> 
> If you wanted the value of either the text or the suffix retained for a second use you can do something like this to save just the text:
> 
>>>> (text := input("enter text: ")).removesuffix(input("enter suffix: ")[1:])
> enter text: hello.avi
> enter suffix: avi
> 'hello.a'
>>>> text
> 'hello.avi'
> 
> Or to save both (and note the extra parentheses):
> 
>>>> (text := input("enter text: ")).removesuffix((suffix := input("enter suffix: "))[1:])
> enter text: hello.avi
> enter suffix: avi
> 'hello.a'
>>>> text
> 'hello.avi'
>>>> suffix
> 'avi'
> 
> Nobody will tell you this is code particularly elegant or easy to read versus a multi-line version and my thoughts may not be helpful if your plans are not as I guessed.
> 
> Another guess maybe a bit further off the wall as if you ask the user for input once and they supply it and at a later time you ask the user for input and the default should be to keep the old one unless they want to change. That can easily be done for example by writing a function like this:
> 
> def default_input(prompt, default=""):
>    if default != "":
>      prompt = prompt + "[" + default + "] "
>    got = input(prompt)
>    if got == "":
>      got = default
>    return(got)
> 
> The above lets you keep asking the user questions and remembers their last choice so future questions can be easier as they are shown the current default and can accept it or replace it.
> 
>>>> result = default_input("What? ")
> What? this
>>>> result
> 'this'
>>>> result = default_input("What? ", result)
> What? [this]
>>>> result
> 'this'
>>>> result = default_input("What? ", result)
> What? [this] that
>>>> result
> 'that'
> 
> Again, probably not what you want but I encourage you to figure out how to ask a more focused question that we can understand and answer properly.
> 
> Q
> 
> 
> 
> 
> Sent with Proton Mail secure email.
> 
> ------- Original Message -------
> On Tuesday, May 16th, 2023 at 4:38 AM, Alan Gauld via Tutor <tutor at python.org> wrote:
> 
> 
>> On 16/05/2023 06:39, Goran Ikac wrote:
>>
>>> If a value is assigned to the string some_string, this works:
>>>
>>>>>> some_string = 'avion'
>>>>>> some_string.removesuffix(input()[1:])
>>>>>> ton
>>>>>> 'avi'
>>
>>
>> It would help us understand what you are trying to do if you added
>> prompts to your input statements:
>>
>> some_string = 'avion'
>> some_string.removesuffix(
>> input("provide suffix with extra character at the front")[1:])
>>
>>> But the next statement doesn't work as I've expected (it waits for another
>>> user's input):
>>
>>
>> Because you have two input statements. It will execute both of them.
>>
>>>>>> input().removesuffix(input()[1:])
>>>>>> avion
>>>>>> ton
>>>>>> 'avi'
>>
>>
>> Why do you want it all on one line? Normally you'd capture the two
>> things then process them:
>>
>> some_string = input("String to process: ")
>> suffix = input("enter a suffix with an extra letter: ")
>> print(some_string.remove_suffix(suffix[1:])
>>
>> Which is easier to read and debug and helps your user.
>>
>>> Now, is there a way to manipulate the input from a user so to remove a
>>> slice from it, other than to assign that slice to a variable name and then
>>> to manipulate that variable?
>>
>>
>> You are not assigning a slice you are applying a slice to the string.
>> It's not clear why you are using a slice but that is not relevant.
>> The real question is why you want to do it all on one line?
>> There is no advantage and several disadvantages.
>>
>> Remember the >>> prompt is not where your real programs will run,
>>
>> that's just for experimenting and testing. The real code will run
>> using print statements etc. And you can place the input/output
>> exactly where you want it.
>>
>>> I'd like to be able to put the user's input as an argument for a method
>>> that manipulates that same user's input. The method .removesuffix() is only
>>> an example.
>>
>>
>> Sorry, I'm still not clear what you really want to do.
>> Can you give us an example of a user session (without code for now)
>> Just show us the inputs and expected outputs?


I'll just add a general comment to the "you probably don't want to pile 
it all in one line" thead:

Once you get used to programming in Python, you won't use the input() 
function very much.  Programs tend to operate on larger sources of data 
- reading data files, pulling in a csv from somewhere, connecting to a 
database, etc.  and very little of the inputs coming to the program will 
be command-line interactive.  Even a GUI-oriented program will use 
higher-level abstractions to get back keypresses, mouse clicks, etc.

When you *do* use it, you'll want to validate the data, because all data 
coming from such an unpredictable source as a human typing at a prompt 
should be assumed to be untrusted.  So your flow will usually be more like:

stuff = input(prompt)
validate(stuff)  # this is an abstract concept, not a Python language 
function!!!
result = do_something(stuff)

That's really messy to do if you're trying to jam it on one line.






More information about the Tutor mailing list