Answer the question
In order to leave comments, you need to log in
How to make it so that when subtracting it does not go into a minus?
import random
player1 = {'player': 'Yarik', 'oranges': 30}
player2 = {'player': 'Egor', 'oranges': 30}
maxkick = 10
kick = random.randint(0, maxkick)
player2 ['oranges'] = player2['oranges'] - kick
print(kick, player2['oranges'])
Answer the question
In order to leave comments, you need to log in
It is obvious that the check will go to minus or not. For example like this:
kick = random.randint(0, maxkick if maxkick < player2['oranges'] else player2['oranges'])
player2['oranges'] = player2['oranges'] - kick
kick
needed .
There are many ways how to do it. You can check before subtracting whether the subtrahend is greater than the minuend. After subtraction, you can check if the value has become less than zero, and reset it to zero. In general, you can make your own type “only positive numbers”, in which you can provide control logic inside so that it does not become less than zero - either reset to zero or return an error.
To choose, you need to understand why and what will happen next.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question