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


notes on previous event:


Exercise 34: Accessing Elements Of Lists


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 ]



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

IndexError: list index out of range


Exercise 35: Branches and Functions


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


# 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)
		dead("Man, learn to type a number!!!! Idiot !!!")

	if how_much < 50:
		print "Nice, you 're not greedy, you win!"
		exit (0)
		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:
			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:
	elif "head" in choice:
		dead("Well that was tasty!")

def dead(why):
	print 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?"

	choice = raw_input("> ")

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


Εντός ύλης

## pydoc sys
        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).