• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

주먹구구식으로 코드를 짰는데 에러가 발생합니다.

20.10.02 13:37 작성 조회수 184

0

while customer_no < 51:
    spent_time = randint(550)
    if spent_time < 16:
        print ("[O] {0}번째 손님 (소요시간 : {1}분)".format(customer_no, spent_time))
        total_customer += 1
        customer_no += 1
    else:
        print ("[ ] {0}번째 손님 (소요시간 : {1}분)".format(customer_no, spent_time))
        customer_no += 1
print ("총 탑승 승객 : {0} 분".format(total_customer))

왜 이러는 걸까요?

답변 2

·

답변을 작성해보세요.

1

답변이 늦어 죄송합니다.

선언되지 않은 변수를 사용하셔서 그렇습니다.

소스코드 맨 위에 아래 3줄을 넣어주시면 될거에요

from random import *
customer_no = 1
total_customer = 0

0

gulsam님의 프로필

gulsam

질문자

2020.11.01

답변 감사합니다! 헌데 for문이나 함수에서는 변수를 따로 선언 하지 않고 바로 생성해도 문제가 없었던 것 같았는데 while문만 그런건가요? for문이랑 함수에서는 지역변수의 형태로 허용되는 건가요?