Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please Help. I would really appreciate it

+2 votes
asked Nov 3, 2022 by Eidnoxon (5,370 points)
The problem is, that i can't substract a value from a class. Example: https://onlinegdb.com/nSGKequzy. Can somebody tell me how to do it?

2 Answers

+1 vote
answered Nov 3, 2022 by Peter Minarik (89,230 points)

This is because you have two instances of the You class. You always create a new instance before you'd interact with it. Instead, you should create a single instance and keep using that one. Like this:

class You:
    hp = 700
you = You()     # creating a single instance
you.hp -= 600   # keep using the same instance
print(you.hp)   # this instance is the same one that got the hp reduced by 600

–1 vote
answered Nov 28, 2022 by Dilipreddy812 (140 points)
class You:
    hp = 700
You().hp = h   
h -= 600
commented Nov 28, 2022 by Dilipreddy812 (140 points)
just assign it an variable
commented Nov 29, 2022 by Peter Minarik (89,230 points)
You're not changing the value stored in the class, just your local copy h.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.