2014年2月10日月曜日

Learn Python the Hard Way 続き 11-15

Exercise 11: Asking Questions

print "How old are you?",
print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()

print "So, you're %r old, %r tall and %r heavy."%(age, height, weight)

x = int(raw_input()) 
y = x +5
print y

Exercise 12: Prompting People

age = raw_input("How old are you?")
height = raw_input("How tall are you?")
weight = raw_input("How much do you weigh?")

print "So, you're %r old, %r tall and %r heavy." %(age, height, weight)

pydoc — Documentation generator and online help system

Exercise 13: Parameters, Unpacking, Variables

from sys import argv

script, first, second, third = argv
fourth = raw_input()

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third
print "Your fourth variable is:", fourth

Exercise 14: Prompting and Passing

from sys import argv

script, user_name = argv
prompt = '>'

print "Hi %s, I'm the %s script." %(user_name, script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes = raw_input(prompt)

print "Where do you live %s?" % user_name
lives = raw_input(prompt)

print "What kind of computer do you have?"
computer = raw_input(prompt)

print """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)

Exercise 15: Reading Files

from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r." % filename
print txt.read()

print "Type the filename again:"
file_again = raw_input(">")

txt_again = open(file_again)

print txt_again.read()
過去ログ:
SF Python Hack Night / Learn Python the Hard Way 1-6
Learn Python the Hard Way 続き 7-10


Disclaimer このブログは山崎富美の個人的なものです。ここで述べられていることは私の個人的な意見に基づくものであり、私の雇用者には一切の関係はありません。