您的位置:首页 > 编程语言 > Python开发

第2章 python实战例子

2016-02-12 16:03 591 查看
1、实战例子:

print("Program 'Game Over' 2.0") #字符串输出

print("Same","message","as before") #打印输出多个值

print("Just", #打印输出多个值,这样输出更好

"a bit",

"bigger")

print("Here",end=" ") #end设置为空格,下次调用从它的右边开始

print("it is...") #从end=" " 右边开始

print("""

╔══╗╔══╗╔══╗╔══╗╔══╗╔╦╗╔══╗╔═╗

║╔╦╣║╔╗║║  ║║ ═╣║╔╗║║║║║ ═╣║║║

║╚╝║║╔╗║║║║║║ ═╣║╚╝║║║║║ ═╣║ ╣

╚══╝╚╝╚╝╚╩╩╝╚══╝╚══╝╚═╝╚══╝╚╩╝

""")

#线体字欢迎输出,多行注释

input("\n\nPrass the enter key to exit.")

#按回车结束

2、实战例子:

# Fancy Credits

#演示转义序列

print("\t\t\tFancy Credits")

print("\t\t\t \\ \\ \\ \\ \\ \\")

print("\t\t\t\tby")

print("\t\t\tMichael Dawson")

print("\t\t\t \\ \\ \\ \\ \\ \\")

print("\nSpecial thanks goes out to:")

print("My hair stylist,Henry \'The Great,\' who never says \"can\'t.\"")

print("\a")

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

3、实战例子:

print("\t\t\t \\ \\ \\ \\ \\ \\ \\")

print("\t\t\t \\ \\ \\ \\ \\ \\ \\")

4、实战例子:

print("\nSpecial thanks goes out to:") #“\n”换行符

5、实在例子:

print("My hair stylist,Henry \'the great,\'who never says \"can\'t.\"")

#插入单引号和双引号

6、实战例子:

print("\a")

#蜂鸣声

7、实战例子:

#"+"使用拼接成一个全新的字符串

#"\"使用继续符连接

print("You can concatenate two" + "strings with the '+' operator.")

print("\nThis string" + "may not" + "seem terr" + "ibly impressive." \

+ "But what" + "you don't know" + " is that\n" + "it's one real" \

+ "l" + "y" + "long string, created from the concatenation" \

+ "of" + "twenty-two\n" + "different strings,broken across" \

+ "six lines." + "Now are you" + " impressed?" + "Okay,\n" \

+ "this" + "one" + "long" + "string is now over!")

#字符串的重复使用“*”符

print("Pie" * 10)

8、实战例子:

#Word Problems

#演示数字和数学运算

#加法运算

input("Press the enter key to find out.")

print("2000 -100 + 50 =",2000 - 100 + 50)

#乘法运算

input("Press the enter key to find out.")

print("6 * 3 =", 6 * 3)

#除法运算(取整数)

input("Press the enter key to find out.")

print("107 // 3 =", 107 // 3)

#取模运算

input("Press the enter key to find out.")

print("107 % 4 =", 107 % 4)

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

9、实战例子:

#Greeter

#演示变量的用法

#声明一个name的变量

name = "Larry"

print(name)

print("Hi,",name)

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

10、实战例子:

#Personal Greeter

#演示获取用户输入

#将“name”赋值给一个语句

name = input("Hi. What's your name?")

print(name)

print("Hi,",name)

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

11、实战例子:

Quotation Manipulation

#演示字符串方法

#IBM主席 Thomas Watson在1943年的时候说的

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")) #将maybe后的字母进行更换

print("\nOriginal quote is still:") #将maybe后的默认字母

print(quote)

print("\nIn swapcase:")

print(quote.swapcase())

print("\nIn capitalize")

print(quote.capitalize())

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

12、实战例子:

# Truse Fund Buddy -Bad

#演示逻辑错误

print("""

╔══╗        ╔╗ ╔╗   ╔═╗        ╔╗╔╗      

║  ║╔═╗╔═╗╔══╗║╚╗║║╔╦╗║═╣╔═╗╔═╗╔═╗╔╝║╠╣╔═╗╔═╗

║║║║║║║║║║╚╗╔╝║║║║║║║║╠═║║║║║╩╣║║║║║║║║║║║║║║

╚╩╩╝╚═╝╚╩╝ ╚╝ ╚╩╝╚╝╠═║╚═╝║╔╝╚═╝╚╩╝╚═╝╚╝╚╩╝╠╗║

               ╚═╝   ╚╝          ╚═╝

""")

car = input("Lamborghini Tune-Ups:")

rent = input("Manhattan Apartment:")

jet = input("Private Jet Rental:")

gifts = input("Gifts:")

food = input("Dining Out:")

staff = input("Staff (butlers,chef,driver,assistant):")

guru = input("Personal Guru and Coach:")

games = input("Computer Games:")

total = car + rent + jet + gifts + food + staff + guru + games

print("\nGrand Total:",total)

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

13、实战例子:

# Truse Fund Buddy -Bad

#演示改后逻辑

print("""

╔══╗        ╔╗ ╔╗   ╔═╗        ╔╗╔╗      

║  ║╔═╗╔═╗╔══╗║╚╗║║╔╦╗║═╣╔═╗╔═╗╔═╗╔╝║╠╣╔═╗╔═╗

║║║║║║║║║║╚╗╔╝║║║║║║║║╠═║║║║║╩╣║║║║║║║║║║║║║║

╚╩╩╝╚═╝╚╩╝ ╚╝ ╚╩╝╚╝╠═║╚═╝║╔╝╚═╝╚╩╝╚═╝╚╝╚╩╝╠╗║

               ╚═╝   ╚╝            ╚═╝

""")

car = int(input("Lamborghini Tune-Ups:"))

car = int(car) #将字符串转换成整型

rent = int(input("Manhattan Apartment:"))

jet = int(input("Private Jet Rental:"))

gifts = int(input("Gifts:"))

food = int(input("Dining Out:"))

staff = int(input("Staff (butlers,chef,driver,assistant):"))

guru = int(input("Personal Guru and Coach:"))

games = int(input("Computer Games:"))

total = car + rent + jet + gifts + food + staff + guru + games

print("\nGrand Total:",total)

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

14、实战例子:

# Useless Trivia

#

#从用户那里获得一些个人信息,然后打印出一些与之前有关的真实而无聊的信息

#获取用户输入信息

name = input("Hi. what's your name?")

age = input("How old are you?")

age = int(age)

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

#打印小写和大写模式

print("\nIf poet se cummings were to email you,he'd address you as",name.lower())

print("But if ee were mad,he'd call you",name.upper())

called = name * 5 #打印name 5次

print("\nIf a small child were trying to get your attention")

print("your name would become:")

print(called)

#计算秒数

seconds = age * 365 * 24 * 60 * 60

print("\n You're over",seconds, "seconds old.")

15、实战例子

#喜欢的食物练习

food1 = input("请输入喜欢的食物:?")

food2 = input("请输入喜欢的食物:?")

print("喜欢的食物分别是:",food1,food2)

16、实战例子:

#小费计算练习

tip1 = input("请输入餐馆的账单:")

tip2 = input("请输入餐馆的账单:")

Fifteen_percent1 = int(tip1) * 0.15

print("百分之十五的小费是:",Fifteen_percent1)

Fifteen_percent2 = int(tip2) * 0.2

print("百分之二十的小费是:",Fifteen_percent2)

17、实战例子:

#汽车消费税计算

car_price = input("请输入汽车总价格:")

Displacement = input("请输入汽车的排气量:")

car_price = int(car_price)

Displacement = int(Displacement)

if Displacement == 1: #排放量为1的需要缴纳1%

print ("应缴纳消费税为:",int(car_price/1.17*0.01))

elif Displacement == 1.5: #排放量为1.5的需要缴纳3%

print ("应缴纳消费税为:",int(car_price/1.17*0.03))

elif Displacement == 2: #排放量为2的需要缴纳5%

print ("应缴纳消费税为:",int(car_price/1.17*0.05))

elif Displacement == 2.5: #排放量为2.5的需要缴纳12%

print ("应缴纳消费税为:",int(car_price/1.17*0.12))

elif Displacement == 3: #排放量为3的需要缴纳25%

print ("应缴纳消费税为:",int(car_price/1.17*0.25))

elif Displacement == 4: #排放量为4的需要缴纳40%

print ("应缴纳消费税为:",int(car_price/1.17*0.4))

else:

print("输入的排气量太大")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: