Learn Python the Hard Way

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

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

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


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

Exercise 29: What If

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


notes on previous event:

https://www.hackerspace.gr/wiki/Learn_Python_the_Hard_Way_12b






Exercise 30: Else and If


#!/usr/bin/python2

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

from sys import argv

people = argv[1]
cars   = argv[2]
trucks = raw_input("Trucks? ")

if cars > people:
	print "We should take the cars."
elif cars < people:
	print "We should not take the cars."
else:
	print "We can't decide."

if trucks > cars:
	print "That's too many trucks."
elif trucks < cars:
	print "Maybe we could take the trucks."
else:
	print "We still can't decide."

if people > trucks:
	print "Alright, let's just take the trucks."
else: 
	print "Fine, let's stay home then."

#

if people > trucks:
	if trucks > cars:
		print "Alright, let's just take the trucks."
	else:
		print "blabla"
else: 
	print "Fine, let's stay home then."

# 

if people > trucks and trucks > cars:
		print "Alright, let's just take the trucks."
else: 
	print "Fine, let's stay home then."


Exercise 31: Making Decisions

#!/usr/bin/python2

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4


def func1():

	name = raw_input("\n\nWelcome to my game!\nWhat's your name? ")
	print "Hello %s" % name
	
	print "Do you want to play: Y/N ?"
	key = raw_input()
	
	if key == 'y' or key == "Y":
		print """
You enter a dark room with 3 doors. 
Do you go through door #1 or door #2 or door #3 ?
Think very carefully.
		"""

		door = raw_input("> ")

		print "I said, think very carefully!"
		door = raw_input("> ")
		

		if door == "1":
			print "There's a giant bear here eating a cheese cake. What do you do?"
			print "1. Take the cake."
			print "2. Scream at the bear."
			
			bear = raw_input("> ")
			
			if bear == "1":
				print "The bear eats your face off. Good Job!"
			elif bear == "2":
				print "The bear eats your legs off. Good job!"
			else:
				print "Well doing %s is probably better. Bear runs away." % bear
		elif door == "2":
			print "You stare into the endless abyss at Cthulhu's retina."
			print "1. Blueberries."
			print "2. Yellow jacket clothspins."
			print "3. Understanding revolvers yelling melodies."
			
			insanity = raw_input("> ")
			
			if insanity == "1" or insanity == "2":
				print "Your body survives powered by a mind of jello. Good job!"
			else:
				print "The insanity rots your eyes into a pool of muck. Good job!"
		
		elif door == "3":
			print "I said, think very carefully. You didnt! You died !"

		else:
			print "You stumble around and fall on a knife and die. Good job!"
		
		if key == 'y' or key == 'Y':
			func1()
		
	else:
		print "Good bye !"


func1()