python 手记4 〖笨方法学python习题19〗

如有意见或其他问题可在下方写下评论或加QQ:**1693121186
欢迎一起讨论技术问题!
代码如下:

# -*- coding: utf-8 -*-
def cheese_and_crackers(cheese_count, boxes_of_crackers): #用def命令创建了函数“cheese_and_crackers”
 print "You have %d cheeses!" % cheese_count #打印字符串与参数cheese_count
 print "You have %d boxes of crackers!" % boxes_of_crackers #打印字符串与参数"boxes_of_crackers"
 print "Man that's enough for a party!" #打印字符串
 print "Get a blanket.\n" #打印字符串并换行


print "We can just give the function numbers directly:" #打印字符串
cheese_and_crackers(20, 30) #赋于函数“cheese_and_crackers”值20与30,将函数cheese_and_crackers的内容打印在屏幕上。


print "OR, we can use variables from our script:" #打印字符串
amount_of_cheese = 10 #赋于变量“amount_of_cheese”值10
amount_of_crackers = 50 #赋予变量“amount_of_crackers”值50

cheese_and_crackers(amount_of_cheese, amount_of_crackers) #赋于函数“cheese_and_crackers”值10与50,将函数cheese_and_crackers的内容打印在屏幕上。


print "We can even do math inside too:" #打印字符串
cheese_and_crackers(10 + 20, 5 + 6) #赋于函数cheese_and_crackers的两个参数“cheese_count, boxes_of_crackers”10+20与5+6的和,将函数cheese_and_crackers的内容打印在屏幕上。


print "And we can combine the two, variables and math:" #打印字符串
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) #赋于函数“cheese_and_crackers”10+100与50+1000的值,函数“cheese_and_crackers”的内容打印在屏幕上。

注释也写上面了,自己钻研吧。
附加练习*

  1. 倒着将脚本读完,在每一行上面天一行注释,说明这行的做用。
  2. 从最后一行开始,倒着阅读每一行,读出所有的重要字符来。
  3. 自己编写至少一个函数出来,然后用10种方式运行这个函数
    代码如下:
# -*- coding: utf-8 -*- 
def ss(apple, iphone):
    print "\n\t\tI have %d apple.The iPhone you took was a real apple!" % apple
    print "\n\t\tSo,your have %d iphone! You really have money!" % iphone


#第一种
x1 = "Do you have an iphone? How many?"
print "\t%s" % x1
x2 = int(raw_input("\tPlease enter how many iphones you have:")) 
ss(x2, 100)

#第二种
ss(10 + 20, 555*5)

#第三种
ss(x2 + 50 * 50, 500)

#第四种
print "\n\t\tPlease enter decimal!"
x0 = int(raw_input("\tPlease enter how many iphones you have:"))
ss(x0, x2)

#第五种
ss(x0 + x2, x0 * x2 * 1000)



def gg(people, girl):
    print "\n\t\tYou can see some %d people." % people 
    print "\n\t\tYou can also see %d girls inside." % girl




#第六种
x0 = int(raw_input("\tPlease enter how many iphones you have:"))
x2 = int(raw_input("\tPlease enter how many iphones you have:")) 
x1 = int(raw_input("\tPlease enter how many iphones you have:"))
gg(x0 + x2, x1 * x2)

#第七种
gg(x0, x1 + x2)

#第八种
gg(x0 * x1 * x2 *1000, x0 * x2 * x1 + 1000)

#第九种
gg(x1, x2)

#第十种
gg(x1 * x2, x1 + x2)

剩下你自己钻研吧,我也不想写了,如果实在不会问我吧。
这习题的内容挺少的,今天就到这了。
建议:自己复制上运行一下,吓死你!

猜你喜欢

转载自blog.csdn.net/hys_ntxif/article/details/78161632