[Tutor] How to manipulate a user's input twice in one statement
ThreeBlindQuarks
threesomequarks at proton.me
Wed May 17 11:33:10 EDT 2023
I see Alan already supplied what I was going to add.
A question that we seem to have is WHY avoiding using an explicit variable is important to you.
Yes, variables with names can persist even when not needed and use a little space but you can remove a variable or contents anytime you want. There is a small chance that the name may be collide with another name or cover it, but that rarely is a problem if you make the name a tad odd.
Alan mentioned that in some cases, the variable called _ is automatically set but use that with care as it gets re-set every time. It is meant to be transient. but this oddball code does work:
input("number: ")
print( _ * int(_), list(range(int(_),0, -1)) )
If I answer 5, I get two outputs where the _ is reused several times and the same for 15:
>>> input("number: ")
number: 5
'5'
>>> print( _ * int(_), list(range(int(_),0, -1)) )
55555 [5, 4, 3, 2, 1]
>>> input("number: ")
number: 15
'15'
>>> print( _ * int(_), list(range(int(_),0, -1)) )
151515151515151515151515151515 [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
In my earlier letter, I offered you ways to use the := operator to instantiate a variable in mid-stream but that would not appeal to you if your goal was to never have a variable.
There are, of course, oddball ways to get several views on a single variable such as a character string. But why bother?
Just as an example, you could make a regular expression that matches something like this in pseudo-english. Match everything as \1 and then match the last few characters as \2 and then in your output, return what was matched in some form that lets you get them independently. So you would feed the output of input() to the regular expression function of your choice and out would come two (or more) things you want with no name that can presumably be swallowed by something else that uses both.
I am not a fan of the above, and will not supply details, just making a point.
What many people might do is to hide lots of details inside a function. Local variables inside go away when done and probably have no namespace collisions if the function is not looking for any outside variables with the same names. Some may even use anonymous functions albeit it can be hard to write them for multi-line constructs.
I think you need to consider that calling input() returns a variable without a name. You have a pointer of sorts in the program that lets you access the contents at that memory location. It has no name so it can only be used in the current context. As soon as you move beyond the currently executing expression, it can be eligible for garbage collection. Using it again elsewhere in your program can normally be done only in one of several ways. One is if you implicitly pass a pointer along and now something else is using it. an example might be the following where no variables were harmed in the creation:
>>> input("TEXT: ").upper().lstrip().rstrip() + " Thank You!"
TEXT: Why am I doing THIS today?
'WHY AM I DOING THIS TODAY? Thank You!'
But the use you contemplate is not as linear but a bit more like remembering what you MEANT without asking it be remembered.
Python has all kinds of ways to do things and in your case, it is possible you could use objects or functional programming that encapsulate your variables in ways you might not consider as creating variables.
Imagine an object that has methods to get input and save them internally as well as methods that use the saved objects to generate deeper results using them. What if each method returns the object (or a copy of it)? Could you make a new object like this and say something like:
myobject.getinput1().getinput2().calculate() and have that return something like you want. No names are visible.
Using functions you can wrap inaccessible names in odd ways but again, why? I bet the people here can keep coming up with ideas including some complex Rube Goldberg ones but I have not seen you supply a motivation of a real need other than some aesthetics.
So I ask if you have experience with any other programming language that has a simple way to do what you want? For example, a stack-based implementation can keep pushing and popping things without explicit names but how easy is it to keep copies of multiple things around just when you need them?
Why would anyone expect Python to trivially support such things?
If you do find a solution that meets your expectations, please post it here for the curious.
Sent with Proton Mail secure email.
------- Original Message -------
On Wednesday, May 17th, 2023 at 2:25 AM, Goran Ikac <goranikac65 at gmail.com> wrote:
> I'd like to manipulate an user's input using a method. The argument for
> that method depends on the value of the user's input. In my previous
> question, I used a slice of the input as an example of a value that depends
> on the value of the input.
> I'm aware that I can get the result this way:
>
> > > > user_input = input('Please, type a word: ')
> > > > to_remove = user_input[1:] # The value of the slice depends
>
> of the value of the input.
>
> > > > result = user_input.removesuffix(to_remove)
>
> # The result depends on both the value of the input, and the value of the
> slice that was applied to the input.
>
> Or this way:
>
> > > > user_input = input('Please, type a word: ')
> > > > result = user_input.removesuffix(user_input[1:])
>
>
> I was curious if I could get the result (that depends on both the value of
> the input, and the value of the slice that was applied to the input)
> without assigning the input to a variable first. Perhaps I could ask the
> same question this way:
>
> > > > user_input = int(input('Please, type a number: '))
> > > > value_to_use = 2 + user_input # This value depends of the value
>
> of the input.
>
> > > > result = 3 * value_to_use
>
> # The result depends on both the input, and the output of the expression
> that used the input as an operand.
>
> Or this way:
>
> > > > user_input = int(input('Please, type a number: '))
> > > > result = 3 * (2 + user_input)
>
>
> That could be written:
>
> > > > result = 3 * (2 + int(input('Please, type a number: '))
>
>
> But, if I need to manipulate the value (that depends on the value of the
> input) by a method, not by an operand or by a function, can I do it without
> assigning that value or/and the value of the input to a variable?
> Perhaps not?
>
> People, I'm learning, not trying to write a useful program, yet. Last time
> when I asked a "pointless" question (NoneType List), I learned an important
> concept from your answers, a concept that I failed to understand properly
> from the textbooks. I appreciate your time very much.
> Love
> Goran from Croatia
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list