while


while의 조건이 True일 때 계속 반복해서 실행한다.

언제까지? while의 조건이 False가 될 때 까지..

distance = 0

while distance < 20:
  print("I'm running",distance,"km")
  distance = distance + 1

반복문과 조건문을 활용한 간단한 게임 (응용)


from random import randint

print("Welcome to Python game")

playing = True
pc_choice = randint(1,50)
while playing:
  user_choice = int(input('choose number'))
  
  if user_choice == pc_choice :
    print('you won')
    playing = False
  elif user_choice < pc_choice :
    print('Higher!')
  elif user_choice > pc_choice :
    print('Lower!')