第二天参考书籍
Python Programming For The Absolute Beginner
说明:
基本按照原书写的,略有更改,文件中添加一些书中的重要内容
可以下载源文件进行学习,没有版权阿。哈哈
基本按照书中出现的顺序!
1,three_year_sim.py
print "\tWelcome to the 'three-year-old simulator'\n"
print "This program simulates a conversation with a three-year-old child."
print "Try to stop the madness.\n"
response=" "
while response!="Because.":
response = raw_input("Why?\n")
print "Oh, Okay."
raw_input("\n\nPress the enter key to exit.") |
2,loosing_battle.py
print "your lone hero is surrounded by a massive army of trolls."
print "their decaying green bodies stretch out, melting into the horizon."
print "your hero unsheathes his sword and begins the last fight of his life.\n"
health=10
trolls=0
damage=3
while health>0:
trolls+=1
health=health-damage
print "your hero swings and defeates an evil troll,but takes", damage ,"damage points.\n"
print "your hero fought valiantly and defeated", trolls, "trolls."
print "but alas, your hero is no more."
raw_input("\nPress the enter key to exit") |
3,maitre.py
print "welcome to the hotel"
print "it seems we are quite full this evening.\n"
money=int(raw_input("how many dollars do you slip the waiter?"))
if money: # equivalent to if money!=0
# it reads more naturally and could be translated to "if there is money"
print "ah, i am reminded of a table, right this way."
else:
print "please, sit. it may be a while."
raw_input("\nPress the enter key to exit.") |
4,finicky_counter.py
count=0
while True: # true is useless
# you must use True represents true. False represents false
count+=1
# end loop if count is greater than 10
if count>10:
break # means "break out of the loop"
# skip 5
if count==5:
continue # means "jump back to the top of the loop
# when count is equal to 5,
# the program does't get to the print count statement
print count
raw_input("\nPress the enter key to exit") |
5,exclusive.py
print "\tExclusive Computer Network"
print "\t\tMembers Only!\n"
security=0
username=''
while not username: # not just works a lot like the word "not"
#putting "not" in front of sth creates a new phrase
# means the opposite of the old one
username=raw_input("Username:")
password=''
while not password:
password=raw_input("Password:")
if username=="Dawson" and password=="secret":
print "Hi, Dawson"
security=5
elif username=="Meier" and password=="civilization":
print "Hey, Meier"
security=3
elif username=="Miyamoto" and password=="mariobros":
print "What's up, Miyamoto"
security=3
elif username=="Wright" and password=="thesims":
# put "and" between two conditions when you want to create a new conditions
# that is true only if both original conditions are ture
print "How goes it, Wright"
security=3
elif username=="guest" or password=="guest":
# a compound condition created with an "or" is true
# as long as at least one of the simpler conditions is ture
# or means "either"
print "Welcome guest"
security=1
else:
print "Login failed.You are not so exclusive.\n"
raw_input("\nPress the enter key to exit.") |
6,guess_my_number.py
import random
print "\tWelcome to 'Guess My Number'!"
print "\tI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"
# set the initial values
the_number=random.randrange(100)+1
guess=int(raw_input("Take a guess:"))
tries =1
# guessing loop
while (guess!=the_number):
if (guess<the_number): #in the original book,it's ">" here
# but ">" is wrong accoring to the meaning
print "lower..."
else:
print "higher..."
guess=int(raw_input("Take a guess:"))
tries+=1
print "you guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
raw_input("\nPress the enter key to exit") |
7,loopy.py
word=raw_input("enter a word:")
print "\nHere is each letter in verticalway in your word:"
for letter in word:
print letter
print "\nThe horizontalway:"
for letter in word:
print letter,
raw_input("\nPress the enter key to exit.") |
8,range.py
print "Counting:"
for i in range(10):
print i,
print "\n\nCounting by fives:"
for i in range(0,50,5): # 50 is the end point, not included
print i,
print "\n\nCounting backwards:"
for i in range(10,0,-1):
print i,
raw_input("\nPress the enter key to exit.") |
9,message_analyzer.py
message=raw_input("enter a message:")
print "\nThe lengthe of your message is:", len(message)
# a sequence's length is the number of elements it has
print "\nThe most common letter in the English language, 'e',"
if "e" in message:
# you can use "in" anywhere in your own programs to chech
# if an element is a member of sequence
print "is in your message"
else:
print "isn't in your message"
raw_input("\nPress the enter key to exit.") |
10,random_access.py
import random
word="index"
print "The word is:", word, "\n"
high=len(word)
low=-len(word)
for i in range(10):
position=random.randrange(low,high)
print "word [", position, "]\t", word[position]
raw_input("\nPress enter key to exit")
# remember don't out of range |
源码文件下载:
|
文件: | 20.tar.gz |
大小: | 3KB |
下载: | 下载 |
|
阅读(963) | 评论(0) | 转发(0) |