Learn Python the Hard Way

From Hackerspace.gr
Jump to: navigation, search
Learn python the hard way.jpg

external link
Starts Organizer
Mon 26 Oct 2015 18:30
Ends Event Owner
Mon 26 Oct 2015 20:30 User:Ebal
  • Κάθε Δευτέρα, 18:30-20:30
  • Συναντήσεις για εκμάθηση προγραμματισμού σε python.

Θα είναι υπό την μορφή study group, δηλαδή όλοι θα μαθαίνουμε παρέα και δεν θα υπάρχει κάποιος "δάσκαλος". Οι συναντήσεις είναι ανοιχτές σε όλες και όλους - αρκεί να φέρεις το laptop σου! Απευθύνονται σε ανθρώπους που θέλουν να ξεκινήσουν με python κι άρα θα είναι αρκετά εισαγωγικές. Η μοναδική δέσμευση είναι να τηρηθεί αυστηρά το ωράριο (δηλαδή εάν κάποιος ή κάποια αργήσει, να μην μας ζητήσει να γυρίσουμε πίσω για να καλύψουμε κάτι που έχουμε ήδη πει).


  • Στις προηγούμενες συναντήσεις έχουμε καλύψει μέχρι και την:

Exercise 33: While Loops

http://learnpythonthehardway.org/book/ex33.html


notes on previous event:

https://www.hackerspace.gr/wiki/index.php?title=Learn_Python_the_Hard_Way_20151012





Exercise 34: Accessing Elements Of Lists

#!/usr/bin/python2

animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus']

print animals[1]
print animals[2]
print animals[0]
print animals[3]
print animals[4]
print animals[2]
print animals[5]
print animals[4]

Εκτός ύλης:


var3 = [ 'one', 10.0, ['a', 'b'], True, [1 ,2 ,3 ], 'file1', [5, 6, 7], blue ]

var3[0]
 'one'

len(var3)
 8

var3[8]
---------------------------------------------------------------------------
IndexError                                
Traceback (most recent call last)
<ipython-input-14-fa1e7d86cefd> in <module>()
----> 1 var3[8]

IndexError: list index out of range

var3[-1]
 'blue'

Exercise 35: Branches and Functions

#!/usr/bin/python2

# from module sys import the exit function
from sys import exit

exit(1)

# define a function with name gold_room and no arguments
def gold_room():
	print "This room is full of gold. How much do you take?"

    # declare variable choice from input
	choice = raw_input("> ")
	
	print "your choice is: %r" % choice

	if "0" in choice or "1" in choice:
		how_much = int(choice)
	else:
		dead("Man, learn to type a number!!!! Idiot !!!")

	if how_much < 50:
		print "Nice, you 're not greedy, you win!"
		exit (0)
	else:
		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:
		choice  = raw_input("> ")

		if choice == "take honey":
			dead("The bear looks at you then slaps your face off")
		elif choice == "taunt bear" and not bear_moved:
			print "The bear has moved from the door. You can go through it now."
			bear_moved = True
		elif choice == "taunt bear" and bear_moved:
			dead("The bear gets pissed off and chews your leg off.")
		elif choice == "open door" and bear_moved:
			gold_room()
		else:
			print "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?"

	choice = raw_input("> ")

	if "flee" in choice:
		start()
	elif "head" in choice:
		dead("Well that was tasty!")
	else:
		cthulhu_room()

def dead(why):
	print why, "Good job!"
	exit(255)

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?"

	choice = raw_input("> ")

	if choice == "left":
		bear_room()
	elif choice == "right":
		cthulhu_room()
	else:
		dead("You stumble around the room until you starve.")

start()


Εντός ύλης

## pydoc sys
"""
    exit(...)
        exit([status])
        
        Exit the interpreter by raising SystemExit(status).
        If the status is omitted or None, it defaults to zero (i.e., success).
        If the status is an integer, it will be used as the system exit status.
        If it is another kind of object, it will be printed and the system
        exit status will be one (i.e., failure).
"""