Can you ignore a previously updated variable and print the former one? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

7th Nov 2023, 7:40 PM
Godson Okafor
Godson Okafor - avatar
12 Answers
+ 12
You could also create a list and append each answer to it if you need to keep track of all answers given.
7th Nov 2023, 9:46 PM
Keith
Keith - avatar
+ 10
I'm confused as to why you would want to do that. Why not just make answer = "yes"?
7th Nov 2023, 7:59 PM
Keith
Keith - avatar
+ 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
7th Nov 2023, 11:58 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 6
you may make another variable to store the previous value answer = 5 previous = answer answer = 2
7th Nov 2023, 8:50 PM
Mafdi
Mafdi - avatar
+ 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])
7th Nov 2023, 10:23 PM
Bob_Li
Bob_Li - avatar
+ 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.
8th Nov 2023, 4:02 AM
Tibor Santa
Tibor Santa - avatar
+ 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.
8th Nov 2023, 4:42 AM
Tibor Santa
Tibor Santa - avatar
+ 3
I know you can do it that way but is there just another way?
7th Nov 2023, 8:28 PM
Godson Okafor
Godson Okafor - avatar
+ 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
8th Nov 2023, 4:24 AM
Bob_Li
Bob_Li - avatar
+ 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.
8th Nov 2023, 6:34 PM
sandra
sandra - avatar
+ 3
Wow, I really learnt a lot.. Thank you all for your responses. let me go and try them out
10th Nov 2023, 6:36 PM
Godson Okafor
Godson Okafor - avatar
+ 2
use if statement?
7th Nov 2023, 9:31 PM
Mohamed Beder Oumar
Mohamed Beder Oumar - avatar