강의

멘토링

커뮤니티

Inflearn Community Q&A

gulsamcono1891's profile image
gulsamcono1891

asked

Free Python Tutorial (Basic) - Become a Developer in 6 Hours

Quiz #5

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

Written on

·

289

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))

왜 이러는 걸까요?

python

Answer 2

1

nadocoding님의 프로필 이미지
nadocoding
Instructor

답변이 늦어 죄송합니다.

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

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

from random import *
customer_no = 1
total_customer = 0

0

gulsam님의 프로필 이미지
gulsam
Questioner

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

gulsamcono1891's profile image
gulsamcono1891

asked

Ask a question