2011-03-15 2 views
0

나는 모든이 정말 새로 온 사람과 지금은 두 가지 기능을 수행하고 인쇄하려고 노력하고있어,하지만 난 그것을 이해 할 수없는 것 :파이썬에서 두 함수를 동시에 사용하려면 어떻게해야합니까?

import datetime 

def get_date(prompt): 
    while True: 
     user_input = raw_input(prompt) 
     try: 

      user_date = datetime.datetime.strptime(user_input, "%m-%d") 
      break 
     except Exception as e: 
      print "There was an error:", e 
      print "Please enter a date" 
    return user_date.date() 

def checkNight(date): 
    date = datetime.strptime('2011-'+date, '%Y-%m-%d').strftime('$A') 

birthday = get_date("Please enter your birthday (MM-DD): ") 
another_date = get_date("Please enter another date (MM-DD): ") 

if birthday > another_date: 
    print "Your friend's birthday comes first!" 
    print checkNight(date) 

elif birthday < another_date: 
    print "Your birthday comes first!" 
    print checkNight(date) 

else: 
    print "You and your friend can celebrate together." 

수 있어야 get_date 기능 날짜가 5 자인지 확인하고 분할이 무엇이든 허용합니다. 또한 누군가가 "02-29"를 입력하면 "02-28"로 취급 할 것입니다. checkNight은 이전 생일이 어느 날 밤에 있는지 확인할 수 있어야합니다.

은 몇 가지 예이다 :

 
Please enter your birthday (MM-DD): 11-25 
Please enter a friend's birthday (MM-DD): 03-05 
Your friend's birthday comes first! 
Great! The party is on Saturday, a weekend night. 

Please enter your birthday (MM-DD): 03-02 
Please enter a friend's birthday (MM-DD): 03-02 
You and your friend can celebrate together! 
Too bad! The party is on Wednesday, a school night. 

Please enter your birthday (MM-DD): 11-25 
Please enter a friend's birthday (MM-DD): 12-01 
Your birthday comes first! 
Great! The party is on Friday, a weekend night. 
+0

코드에 표현하고 있지만 표현할 수없는 것을 적어 두십시오. 그것 없이는, 그것은 질문이 아니라 작업 묘사입니다. –

+0

파이썬 튜토리얼을 사용하면 http://wiki.python.org/moin/BeginnersGuide/Programmers를 시작할 수 있습니다. – MattiaG

답변

2
  • 한 오류가 정의 된 변수 "날짜"를 호출하지 않고 checkNight(date) 때문이다.
  • datetime.strptimedatetime.datetime.strptime이어야합니다.
  • 같은 줄에 문자열과 날짜를 연결하면 ('2011-'+date) 오류가 발생할 수도 있습니다.
  • checkNight(date) 기능은 아무것도 반환하지 않습니다

어쩌면 이것은 당신이 원하는에 좀 더 가까이이다 :

import datetime 

def get_date(prompt): 
    while True: 
     user_input = raw_input(prompt) 
     try: 
      user_date = datetime.datetime.strptime(user_input, "%m-%d") 
      user_date = user_date.replace(year=2011) 
      break 
     except Exception as e: 
      print "There was an error:", e 
      print "Please enter a date" 
    return user_date 

def checkNight(date): 
    return date.strftime('%A') 

birthday = get_date("Please enter your birthday (MM-DD): ") 
another_date = get_date("Please enter another date (MM-DD): ") 

if birthday > another_date: 
    print "Your friend's birthday comes first!" 
    print checkNight(another_date) 

elif birthday < another_date: 
    print "Your birthday comes first!" 
    print checkNight(birthday) 

else: 
    print "You and your friend can celebrate together." 

주를 그 나는 2011 년 직후를 변경하기 때문에 사용자가 입력하면 더 간단하게 checkNight()에 요일을 추출 할 수 있습니다.

관련 문제