0
Can you ignore a previously updated variable and print the former one?
Like what can you do in; Answer="yes" Answer="no" For print (answer) to be equal to yes
12 ответов
+ 12
You could also create a list and append each answer to it if you need to keep track of all answers given.
+ 10
I'm confused as to why you would want to do that. Why not just make answer = "yes"?
+ 7
Bob_Li Keith
The idea of using list to store the variable history is interesting. It just like the "back \ previous" button in a web browser, the list stores the visited webpage in order.
With a negative index N we can recall the previous N value.
[EDITED]
Example code
https://code.sololearn.com/c4q5BhXSQFv4/?ref=app
+ 6
you may make another variable to store the previous value
answer = 5
previous = answer
answer = 2
+ 6
Godson Okafor
I agree with Keith .
if you want a history of values, you could store them in a list.
answers = ['yes',]
print(answers[-1])
print('-'*30)
answers.append('no')
print(answers[-1])
print(answers[-2])
print('-'*30)
answers.append('maybe')
print(answers[-1])
print(answers[-2])
print(answers[-3])
+ 6
The normal behavior is when you change a variable, the old value is discarded and forgotten, basically it becomes 'garbage' in memory.
There are many ways to solve it. If you want to preserve the value before the last change, it may be enough to create a tuple, or namedtuple, or dataclass that keeps track of only two values: current and previous.
It is also possible to write a class with a property, where you customize the setter to store the previous value before overwriting it, and you can write a rollback or undo method that restores the previous state.
For more complex applications and especially in purely object oriented languages, the Memento design pattern was meant to solve this.
https://refactoring.guru/design-patterns/memento
Also it can be another solution to save previous state(s) in a database.
In Python this can be managed in simpler ways. The point is, that you should really explain why you would want to do this, what is the goal of the specific application and what are the circumstances.
+ 4
Bob_Li yes Command and Memento are related and even the article says that both of them can be used combined to implement the undo/redo.
I think the main difference is that Command encapsulates an action (behavior), and Memento encapsulates a sequence of states.
+ 3
I know you can do it that way but is there just another way?
+ 3
Tibor Santa
memento pattern is a new one for me. I did learn about command pattern for undo/redo history before. It is also mentioned in your link..
https://refactoring.guru/design-patterns/command
+ 3
Godson Okafor,
What about using a kind of stack. This is a so called LIFO, which is a data structure that means "Last In First Out". The stack can hold multiple members.
A stack can be implemented with a list, or we can use the "deque" data structure from the Collections module.
+ 3
Wow, I really learnt a lot..
Thank you all for your responses. let me go and try them out
+ 2
use if statement?