Probability = 0.52;
SharePrice = 215;
n = 10000;
count = 0;
count250 = 0;
count200 = 0;
for i = 1:n
CoinToss = rand(1);
if (SharePrice == 200)
break;
end
if (SharePrice == 250)
break;
end
if(Probability < CoinToss)
SharePrice = SharePrice - 0.2;
else
SharePrice = SharePrice + 0.2;
end
end
代码似乎超出了我给出的限制(200和250),为什么它不会停止?
答案 0 :(得分:0)
欢迎浮点不准确;您的handle_event
可能最多不能合计200,因此它最终会导致import pygame as pg
from pygame.math import Vector2
class Player(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(*groups)
self.image = pg.Surface((30, 50))
self.image.fill(pg.Color('steelblue2'))
self.rect = self.image.get_rect(center=pos)
self.vel = Vector2(0, 0)
self.pos = Vector2(pos)
def update(self):
self.pos += self.vel
self.rect.center = self.pos
class Bullet(pg.sprite.Sprite):
def __init__(self, pos, velocity):
super().__init__()
self.image = pg.Surface((5, 5))
self.image.fill(pg.Color('aquamarine1'))
self.rect = self.image.get_rect(center=pos)
self.pos = pos
self.vel = velocity
def update(self):
self.pos += self.vel
self.rect.center = self.pos
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
bullets = pg.sprite.Group()
player = Player((100, 300), all_sprites)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player.vel.x = 5
elif event.key == pg.K_SPACE:
# Just create a bullet and add it to the sprite groups.
bullet = Bullet(player.rect.center, Vector2(8, 0))
bullets.add(bullet)
all_sprites.add(bullet)
elif event.type == pg.KEYUP:
if event.key == pg.K_d:
player.vel.x = 0
all_sprites.update()
screen.fill((30, 30, 30))
all_sprites.draw(screen)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
条件失败(同样适用于SharePrice
)。
如果SharePrice == 200
低于200或高于250,您应该考虑SharePrice == 250
离开循环:
break