Chinaunix首页 | 论坛 | 博客
  • 博客访问: 367008
  • 博文数量: 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-19 21:41:38

参考书籍:
python programming for the absolute beginner


第一个:craps_roller.py
import random

# generate random numbers 1 to 6
die1 = random.randrange(6)+1
die2 = random.randrange(6)+1

total=die1+die2
print  "you rolled a ", die1, "and a", die2, "for a total of", total
die3=random.randrange(6)+1
print die3
# now, die3 gets either a 1, 2, 3, 4, 5or 6

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

因为range(6)产生的是0-5,故要+1
用import random ,后面要点操作,前简后繁

第二个:dog_years.py
age=int(raw_input("please enter your age:"))
import math
dog_year=math.floor(age/7)

print "do you know you are ", dog_year, "in dog years"
second=age*365*24*60*60
print "but you are also " ,second,"seconds old"
		
raw_input("please press the enter key to exit")
get_personal_info.py
name = raw_input("Hi, What's your name?")
age = raw_input( "And how old are you?")
age=int(age)

weight = raw_input("Okay, last question. How many pounds do you weight?")
weight = int(weight)

print  "this is your personal information:"
print  "name", name
print  "age", age
print "weight", weight

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

greet.py

name = "Larry"

print name

print "Hi," + name

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

if_elif.py
import random
print "I sense your energy.Your true emotions are coming across my screen."
print "You are..."

mood=random.randrange(3)

if mood==0:
	# happy
	print "*_*"
elif mood==1:
	# neutral
	print "***"
elif mood==2:
	# sad
	print "*>*"
else:
	print "Illegal mood value! (You must be in a really bad mood."
print "...today."	
raw_input("\nPress the enter key to exit.")

moon_sun_weight.py
weight=int(raw_input("please enter your weight:"))
moon_weight = weight / 6.0
sun_weight = weight * 27.1

print "\nOn the moon, you would weight only", moon_weight, "pounds"
print "\nBut on the sun, you would weight ",sun_weight,"pounds"

raw_input("please press the enter key to exit")

password.py
print  "Welcome to System Security Inc!"
print "--where security is our middle name\n"
password=raw_input("Enter your password:")

if password =="secret":
	print "Access Granted"
	print "Welcome! You must be someone very important."
else:
	print "Access Denied"

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

quotes.py
quote = "I think there is a world market for maybe five computers."
print "Original quote:"
print quote

print "\nIn uppercase:"
print quote.upper()

print "\nIn lowercase:"
print quote.lower()

print "\nAs a title:"
print quote.title()

print "\nWith a minor replacement:"
print quote.replace("five","millions of")

print "\nOriginal quote is still:"
print quote

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

学习的革命一书也提到这句经典的话!

trust_fund_buddy.py
car =int( raw_input("Lamborghini Tune-Ups:"))
rent = int(raw_input("Manhattan Apartment:"))
jet = int(raw_input( "Private Jet Rental:"))
gifts = int(raw_input("Gifts:"))
food = int(raw_input("Dining Out:"))
staff = int(raw_input( "Staff (butlers, chef, driver, assistant):"))
guru = int(raw_input( "Personal Guru and Coach: "))
games = int(raw_input( "Computer Games: "))
total = car + rent +jet +gifts +food + staff+ guru +games

print  "\nGrand Total: ",  total

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

注意类型不同,不能乱加!
代码文件下载
文件:pythonscript.tar.gz
大小:2KB
下载:下载

阅读(973) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~