2016-10-23 2 views
-1

if 문을 사용하지 않고 프로그램을 편집하려면 어떻게해야합니까?파이썬을 사용하지 않고 튀는 공

from visual import * 

display(title='BasicEdit', x=0, y=0, fullscreen=True, 
     center=(0,0,0), range=(10,10,10), 
     background=(0,0,0)) 
x_axis = arrow(pos=(0,0,0), axis = vector(3,0,0), color=color.green) 
y_axis = arrow (pos=(0,0,0), axis = vector(0,3,0), color=color.red) 
z_axis = arrow(pos=(0,0,0), axis = vector(0,0,3), color=color.cyan) 
ball = sphere(pos=(0,4,0), radius = 0.4, color=color.red) 
ball.velocity = vector(0,-1,0) 
ground = box(pos = (0,-0.1,0), lenght = 50, height = 0.1, width = 50) 

dt = 0.01 
while 1: 
    rate(100) 
    ball.pos = ball.pos + ball.velocity*dt 
    if ball.y < 1: 
     ball.velocity.y = -ball.velocity.y 
    else: 
     ball.velocity.y = ball.velocity.y - 9.8*dt 
+1

: if/else를 사용하지 않고

if ball.y < 1: ball.velocity.y = -ball.velocity.y else: ball.velocity.y = ball.velocity.y - 9.8 * dt 

은 다음과 같이 작성 될 수 있을까? –

답변

1

나는 당신이, 당신이 그것에 대해 and/or 조합을 사용할 수 있습니다 같은 논리를 유지 코드에서 if/else 조건을 제거 할 생각합니다. 예를 들어, 코드 : 당신은 당신의 코드에서/else` 조건이 같은 논리를 유지하는 경우`제거 할

ball.velocity.y = (ball.y < 1 and -ball.velocity.y) or (ball.velocity.y - 9.8 * dt) 
관련 문제