Я посылаю своего садовника собирать яблоки с дерева. Я даю ему 3 попытки поймать красное яблоко и 2 попытки поймать зеленое. Он должен повторить этот процесс 4 раза.
ПРОБЛЕМА: Почему он иногда пытается поймать зеленое яблоко 3 раза?
import random
def garden():
green_apple = 0
red_apple = 0
tree = 50
for _ in range(4):
for _ in range(3):
x = random.randint(1,10)
if x >= 7:
tree -= 1
print('You catch an red apple! There are ' + str(tree) + ' apples left on the tree')
red_apple += 1
if x <= 6:
print('No red apples this time')
for _ in range (2):
y = random.randint(1,10)
if y >= 5:
tree -= 1
print('You catch an green apple! There are ' + str(tree) + ' apples left on the tree')
green_apple += 1
if y <=5:
print('No green apples this time')
print('################################################')
return (red_apple, green_apple)
green_bag, red_bag = [], []
for _ in range(3):
green_apple, red_apple = garden()
green_bag.append(green_apple)
red_bag.append(red_apple)
print(red_bag)
print(green_bag)
ОБРАЗЕЦ:
You catch an green apple! There are 39 apples left on the tree
You catch an green apple! There are 38 apples left on the tree
No green apples this time
################################################
Почему?