원문 : http://learnpythonthehardway.org/book/ex35.html
조금 복잡한 if 문, 다양한 함수를 가지고 간단한 게임을 만들어보자.(ex35.py)
(※ 실제로 실행시키는것 보다 주석으로 함수의 기능과 결과를 예측해보자)
from sys import exit def gold_room(): print "This room is full of gold. How much do you take?" next = raw_input("> ") if "0" in next or "1" in next: how_much = int(next) else: dead("Man, learn to type a number.") if how_much < 50: print "Nice, you're not greedy, you win!" exit(0) else: dead("You greedy bastard!") # gold_room 함수 # "This room is full of gold. How much do you take?" 출력 # next 변수를 입력받아 0 또는 1이 입력되면 how_much에 저장, 그렇지 않으면 "Man, learn to type a number." 출력 # how_much가 50보다 작으면 "Nice, you're not greedy, you win!" 출력 하고 프로그램 종료, 그렇지 않으면 dead("You greedy bastard!") 함수 실행 def bear_room(): print "There is a bear here." print "The bear has a bunch of honey." print "The fat bear is in front of another door." print "How are you going to move the bear?" bear_moved = False while True: next = raw_input("> ") if next == "take honey": dead("The bear looks at you then slaps your face off.") elif next == "taunt bear" and not bear_moved: print "The bear has moved from the door. You can go through it now." bear_moved = True elif next == "taunt bear" and bear_moved: dead("The bear gets pissed off and chews your leg off.") elif next == "open door" and bear_moved: gold_room() else: print "I got no idea what that means." # bear_room 함수 # 문자열 인쇄 # next를 입력받아서 "take honey" 이면 dead("The bear looks at you then slaps your face off.") 실행 # "taunt bear"이고, bear_moved 가 False 이면 "The bear has moved from the door. You can go through it now." 인쇄 후 bear_moved = True # "taunt bear"이고, bear_moved 가 False 이면 dead("The bear gets pissed off and chews your leg off.") 실행 # "open door"이고, bear_moved 가 False 이면 gold_room() 실행 # 그렇지 않으면 "I got no idea what that means." 인쇄 def cthulhu_room(): print "Here you see the great evil Cthulhu." print "He, it, whatever stares at you and you go insane." print "Do you flee for your life or eat your head?" next = raw_input("> ") if "flee" in next: start() elif "head" in next: dead("Well that was tasty!") else: cthulhu_room() # cthulhu_room 함수 # 문자열 인쇄 # "flee"가 포함된 문자열이면 start() 실행, "head"가 포함된 문자열 이면 dead("Well that was tasty!") 실행, 그렇지 않으면 cthulhu_room() 실행 def dead(why): print why, "Good job!" exit(0) # dead(why)변수 # 입력받은 변수(why) 와 "Good job!" 출력 # 프로그램 종료 def start(): print "You are in a dark room." print "There is a door to your right and left." print "Which one do you take?" next = raw_input("> ") if next == "left": bear_room() elif next == "right": cthulhu_room() else: dead("You stumble around the room until you starve.") # start 함수 # 문자열 인쇄 # "left" 가 입력되면 bear_room() 실행, "right"가 입력되면 cthulhu_room() 실행 # 그렇지 않으면 dead("You stumble around the room until you starve.") 실행 start() # start() 실행~ - 사실상 프로그램의 시작점
결과는 이것저것 입력하면서 직접 확인해 보자.
반응형
'Python > Python 어렵게 배우기' 카테고리의 다른 글
Python 어렵게 배우기 - Exercise 37: Symbol Review (0) | 2014.01.03 |
---|---|
Python 어렵게 배우기 - Exercise 36: Designing and Debugging (0) | 2014.01.03 |
Python 어렵게 배우기 - Exercise 34: Accessing Elements of Lists (0) | 2013.12.30 |
Python 어렵게 배우기 - Exercise 33: While Loops (0) | 2013.12.27 |
Python 어렵게 배우기 - Exercise 32: Loops and Lists (0) | 2013.12.27 |