다음 함수를 작성했습니다. 변수는 input_name
입니다. 사용자는 input_name
에 할당 된 값을 입력합니다. 기능 밖에서 input_name
에 액세스 할 수있는 가장 좋은 방법을 알고 싶습니다. 함수 내에서 변수를 전역 변수로 정의한다는 것은 함수 밖에서 변수를 사용할 수 있음을 의미합니다. 그러나이 경우 변수는 실제로 함수의 매개 변수이므로 전역 변수로 정의하는 방법을 모르겠습니다. 나는이 어떤 도움을 주셔서 감사합니다, 아래 문제의 코드를 찾을하십시오파이썬 함수 매개 변수를 전역 변수로 사용
def input(input_name, prompt):
while True:
data = raw_input(prompt)
if data:
try:
input_name = int(data)
except ValueError:
print 'Invalid input...'
else:
if input_name >= 0 and input_name < 100:
print 'Congratulations'
break
input_name = 'Please try again: '
else:
print 'Goodbye!'
break
month = 0
day = 0
year = 0
century = 0
input(month, "Please enter the month (from 1-12, where March is 1 and February is 12): ")
input(day, "Please enter the day (from 1-31): ")
input(year, "Please enter the year (from 0 - 99, eg. 88 in 1988): ")
input(century, "Please enter the century (from 0 - 99, eg. 19 in 1988): ")
A = month
B = day
C = year
D = century
사이드 노트 :'input_name = '다시 시도하십시오 :''- 아마'input_name'이 아닌'prompt'를 설정하고 싶을 것입니다. – poke