Chinaunix首页 | 论坛 | 博客
  • 博客访问: 377702
  • 博文数量: 97
  • 博客积分: 2846
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-19 20:00
文章分类

全部博文(97)

文章存档

2017年(1)

2013年(2)

2012年(6)

2011年(17)

2010年(12)

2009年(41)

2007年(18)

我的朋友

分类: Python/Ruby

2007-03-21 17:10:46

已经是第三天了 。感觉真是不错。
渐渐的 有 点 感觉了 。每天看近4小时,还真有点累阿!
再说一次,这些都是俺自己打的,略有改动,文件中加如解释比较多。
代码越来越长了。呵呵
1,no_vowels.py
message=raw_input("Enter a message:")
new_message=''
VOWELS='aeiou'

print
for letter in message:
	if letter.lower() not in VOWELS:
		# make sure only lowercase letters when using "in" operator
		new_message+=letter
		print "A new string has been created.", new_message
print "\nYour message without vowels is:", new_message

raw_input("\n\nPress the enter key to exit.")

2,pizza.py

word='pizza'

print "enter the beginning and ending index for your slice of 'pizza'"
print "press the enter key at 'begin' to exit."

begin=None                   # None is python's way of representing nothing
                                        # evaluates to false when treated as a condition
while begin != '':
	begin=(raw_input("\nBegin:"))
	if begin:
		begin=int(begin)
		
		end=int(raw_input("End:"))
		print "word[", begin, ":", end, "]\t\t",
		print word[begin:end]
raw_input("\nPress the enter key to exit.")

3,inventory.py

inventory=()

# treat the tuple as a condition
if not inventory:
	print "you are empty-handed."
raw_input("\nPress the enter key to continue.")

# create a tuple with some items
inventory=("sword",
			"armor",
			"shield",
			"healing potion")
# you can write a tuple in one line
# or span it across multiple lines
# make your programs easier to read by creating tuples
# across multiple lines

# print the tuple
print "\nThe tuple inventory is:\n", inventory

# print each element in the tuple
print "\nYour items:"
for item in inventory:
	print item

raw_input("\nPress the enter key to exit.")

4,inventory2.py

inventory = ("sword",
			"armor",
	   		"shield",
	      		"healing potion")
print " Your items : "
for item in inventory :
	print item
raw_input ( " \nPress the enter key to continue")

# get the length of the inventory
print " You have ", len(inventory), " items in your possesion."
raw_input ( " \nPress the enter key to continue")

# test for membership with in
if "healing potion" in inventory:
	print " You will live to fight another day."

# display one item through an index
index = int ( raw_input ( "\nEnter the index number for an item in inventory : "))
print "At index", index, "is", inventory[index]

# display a slice
begin = int ( raw_input (" \nEnter the index number to begin a slice: "))
end = int ( raw_input ("\nEnter the index number to end the slice:"))
print "inventory[", begin, " : " , end , "]\t\t",
print inventory [begin : end]

raw_input ( "\nPress the enter key to continue")

# concatenate two tuples
chest = ( "gold", "gems")
print  "You find a chest. It contains :"
print chest
print "You add the contents of the chest to your inventory"
inventory+=chest
print "Your inventory is now:"
print inventory

raw_input("\n\nPress the enter key to exit.")

5,word_jumble.py
import random
# create a sequence of words to choose from 
WORDS=( "python",
		"jumble",	
	  	"easy",
	    	"difficult",
	      	"answer",
		"xylophone")
# use random.choice() to grab a random word form WORDS
# pick one word randomly from the sequence
word = random.choice(WORDS)
# this function generates strings in WORDS

# assign word to correct to see the player makes a correct guess
# create a variable to use later to see if the guess is correct
correct = word
# create a jumbled version of word
jumble =" "
while word:
	position = random.randrange(len(word))
	# create a new version of the string jumble
	jumble+=word[position]
	word = word[: position] + word[ (position+1) :]
	
# star the game
print \
"""
				Welcome to word Jumble!
		Unscramble the letters to make a word.
(press the enter key at the promt to quit.)
"""
print " The jumble is:", jumble

# getting the player's guess
guess = raw_input("\nYour guess : ")
guess = guess.lower()
while (guess!=correct) and (guess!=" "):
	print "sorry, that's not it."
	guess=raw_input("Your guess:")
	guess = guess.lower()
if guess==correct:
	print "That's it! You guessed it!\n"
	
# end the game
print "Thanks for playing"
raw_input( "\n\nPress the enter key to exit.")

6,inventory3.py
inventory = [ "sword", "armor", "shield", "healing potion"]
print " Your items:"
for item in inventory:
	print item
raw_input("\nPress the enter key to continue.")

# get the length of a list
print "You have", len(inventory), "items in your inventory"
raw_input("\nPress the enter key to continue")

# test for membership with in
if "healing potion" in inventory:
	print "You will live to fight another day."

# display one item through an index
index=int(raw_input("\nEnter the index number for an item in inventory:"))
print "At index", index, "is", inventory[index]

# display a slice
begin=int(raw_input("\nEnter the index number to begin a slice:"))
end=int(raw_input("\nEnter the index number to end a slice:"))
print "inventory[", begin, ":", end, "]\t\t",
print inventory[begin:end]

# concatenate two lists
chest=["gold", "gems"]
print "You find a chest which contains:"
print chest
print "You add the contents of the chest to your inventory"
inventory+=chest
print "Your inventory is now:"
print inventory
raw_input("\nPress the enter key to continue")

# assign by index
print "You trade your sword for a crossbow"
inventory[0]="crossbow"
print "Your inventory is now:"
print inventory
raw_input("\nPress the enter key to continue")

# assin by slice
print "You use your gold and gems to buy an orb of future telling."
inventory[4:6] =["orb of future telling"]
print "Your inventory is now:"
print inventory
raw_input("\nPress the enter key to continue")

# delete an element
print "In a great battle , your shield is destroyed."
del inventory[2]
print "Your inventory is now:"
print inventory
raw_input("\nPress the enter key to continue")

# delete a slice
print "Your crossbow and armor are stolen by thieves."
del inventory[:2]
print "Your inventory is now:"
print inventory
raw_input("\n\nPress the enter key to exit.")

7,shared_references.py

mike=["khakis", "dress shirt", "jacket"]
mr_dawson=mike
honey=mike
print "\na friend calls me Mike, I'm wearing",
print mike
print "\na dignitary calls me Mr Dawson, I'm wearing",
print mr_dawson
print "\nmy girlfirend calls me Honey, I'm wearing",
print honey
# all three variables, mike mr_dawson, and honey, 
# refer to the same, single list, representing me
# at least what i'm wearing

honey[2]="red sweater"
print "\nmy girlfirend asks me to change my jacket for a red sweater now, i'm wearing",
print honey
print "\nif a friend meets me, he calls me mike, and i'm wearing",
print mike
print "\nif a dignitary meets me, he calls me Mr Dawson, and i'm still wearing",
print mr_dawson

mike=["khakis", "dress shirt", "jacket"]
honey=mike[:]
 # honey is assigned a copy of mike
 # honey doesnot refer to the same list
 # just as i was cloned
 # one at home with my girlfirend
 # one at my work place with my friend
honey[2]="red sweater"
print "\nat home a cloned me  is wearing",
print honey
print "\nbut at my work place, i'm still wearing",
print mike

8,high_scores.py

scores =[92,92,87,90,85,93,96,95,98]
choice =None
while choice !="0":
	print \
	"""
	High Scores Keeper
	
	0	exit
	1	show scores
	2	add a score
	3	delete a score
	4	sort scores
	"""
	choice=raw_input("Choice:")
	print

	# exit
	if choice =="0":
		print "goodbye"
	# list high-score table
	elif choice=="1":
		print "high scores"
		for score in scores:
			print scores
	# add a scores
	elif choice=="2":
		score=int(raw_input("what score did you get?:"))
		scores.append(score)
		print "your new scores are:", scores
	# delete a score
	elif choice =="3":
		score=int(raw_input("Delete which score?:"))
		if score in scores:
			scores.remove(score)
			print "Your new scores are:", scores
		else:
			print score, "isn't in the high scores list."
	# sort scores
	elif choice=="4":
		scores.sort()
		print "the scores in acending order  are:",scores
		scores.reverse()     # want the highest number first
		print " in reverse, the scores are:", scores
	# some unknow choice
	else:
		print "sorry, but", choice, "isn't a valid choice."
	
raw_input("\n\nPress the enter key to exit.")

9,high_scores2.py

scores=[("A",1000),  ("D", 4000),("C", 3000) ,("E", 5000),("B",2000),]
print  scores

choice=None
while choice!="0":
	print \
	"""
	High Scores Keeper
	0	quit
	1	list scores
	2	add a score
	"""
	choice=raw_input("Choice:")
	print

	# exit
	if choice=="0":
		print "Goodbye!"
	
	# display high-score table
	elif choice=="1":
		print "NAME\tSCORE"
		for entry in scores:
			name,score=entry
			print name, "\t", score 

	# add a score 
	elif choice=="2":
		name=raw_input("what is the player's name?:")
		score=int(raw_input("what score did the player get?:"))
		entry=( name,score)
		scores.append(entry)
		print "\nThe new scores are:",scores
		scores.sort()
		print "\nAscending order", scores
		scores.reverse()
		print "\nDescending order",scores
		scores=scores[:5]
		print "\nthe first FOUR",scores
	
	# some unknow choice
	else:
		print "sorry, but", choice, "isn't a valid choice"
	
raw_input("\nPress the enter key to ex it")

10,geek_translator.py

geek = { "404": "Clueless. From the web error message 404, meaning page not found.",
		"Googling": "Searching the Internet for background information on a person.",
		"Link Rot" : "The process by which web page links become obsolete.",
		"Keyboard Plaque": "The collection of debris found in computer keyboards.",
		"Percussive Maintenance": " The act of striking an electronic device to make it work.",
		"Uninstalled" : "Being fired. Especially popular during the dot-bomb era."}
print "the dictionary is:",geek

# using a key to retrieve a value
print "\nThe value of dictionary geek\'s key '404' is :",
print geek["404"]
print "\nThe value of geek\'s  key 'Link Rot' is:",
print geek["Link Rot"]
# dictionaries don't have position numbers at all

if "Dancing Baloney" in geek:
	print "\nI know what Dancing Baloney is."
else:
	print "\nI have no idea what Dancing Baloney is."

print "\nAs 'Dancing Baloney' is not in geek, so when retrieve it ,you will get:",
print geek.get("Dancing Baloney", "i have no idea.")
print "\nAs '404' is in geek, so when you retrieve it, you will get:",
print geek.get("404")

choice=None
while choice!="0":
	print \
	"""
	Geek Translator
	0	quit
	1	look up a geek term
	2	add a geek term
	3	redefine a geek term
	4	delete a geek term
	
	"""
	choice=raw_input("Choice:")
	print
	
	# exit
	if choice=="0":
		print "goodbye"
	# get a definition
	elif choice=="1":
		term=raw_input("what term do you want me to translate?")
		if term in geek:
			definition=geek[term]
			print "\n", term, "means", definition
		else:
			print "\nSorry, I don't know", term
	# add a term-definition pair
	elif choice=="2":
		term=raw_input("what term do you want me to add?:")
		if term not in geek:
			definition=raw_input("what's the definition?:")
			geek[term]=definition
			print "\n", term, "has been added."
			print "\nNow the new geek is :", geek
		else:
			print  "\nThat term already exists! Try redefining it."
	# redefine an existing term
	elif choice=="3":
		term=raw_input("what term do you want me to redefine?:")
		if term in geek:
			definition=raw_input("what's the new definition?:")
			geek[term]=definition
			print "\n", term, "has been redefined"
			print geek
		else:
			print "\nThat term doesn't exist! Try adding it."
	# delete a term-definition pair
	elif choice=="4":
		term=raw_input("what term do you want me to delete?:")
		if term in geek:
			del geek[term]
			print "\nOK, I deleted", term
		else:
			print "\nI can't do that!", term, "doesn't exist in the dictionary."
	# some unknow choice
	else:
		print "\nSorry, but", choice, "isn't a valid choice."
raw_input("\nPress the enter key to exit.")


下载文件:

阅读(1262) | 评论(1) | 转发(0) |
0

上一篇:python script 10-20

下一篇:python scripts 连载4

给主人留下些什么吧!~~