LPTHW,EX19额外功劳3

时间:2012-02-12 23:21:30

标签: python

我正在做Zed Shaw的LPTHW书,但是我被这个额外的学分3 question所困,并且发现这里没有任何问题,所以我决定开个帐户。

额外信用分配 至少编写一个自己设计的功能,并以10种不同的方式运行。

代码:

def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print "You have %d cheeses!" % cheese_count
    print "You have %d 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)


print "OR, we can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, amount_of_crackers)


print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)


print "And we can combine the two, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

那么,在脚本中运行函数的其他方法是什么?能否以初学详细的方式帮助我,包括实际代码,以便我可以尝试理解它?

6 个答案:

答案 0 :(得分:2)

我正在将评论合并到这里的答案中。

print 'We can assign the function to a variable and simply call it by its new name'
foo = cheese_and_crackers
foo(20,30)

print 'We can call the function in a loop, calling it 10 times'
for i in range(10):
    cheese_and_crackers(20,30)

print 'We can pass a function as arguments.'
print 'We can now ask the user for the number of cheese and crackers'
cheese_and_crackers(int(raw_input('how many cheese?')), int(raw_input('how many crackers?')))

答案 1 :(得分:1)

我可以添加更多方法:

<img>

答案 2 :(得分:0)

from sys import argv

a, u = argv

def get_length(arg):
    return len(arg)

# 1
print get_length(u)
# 2
print get_length(u+'leaf')
# 3
print get_length(u+'5'*4)
# 4
print get_length('length\n')
# 5
print get_length('length'+'python')
# 6
print get_length('length'*6)
# 7
temp = 'length'
print get_length(temp)
# 8
print get_length(temp+'python')
# 9
print get_length(temp*6)
# 10
print get_length(open(raw_input('filenanme: ')).read())
# 11
print get_length(raw_input('a string:'))

答案 3 :(得分:0)

我们也可以使用命令行参数:

from sys import argv
script, arg1, arg2 = argv

cheese_and_crackers(int(arg1), int(arg2))

或者从文件中读取两个输入:

txt = open('filename.txt')
arg1 = int(txt.readline())
arg2 = int(txt.readline())
cheese_and_crackers(arg1, arg2)

答案 4 :(得分:0)

我是初学者,我也在做ex19。也许您只是想太多了。我认为Zed的目的只是告诉我们:“尝试使用ex19之前告诉您的内容”,因此,我只是将ex19之前学到的内容结合起来,采用10种不同的方式。我不能提供任何建议,仅是我自己的工作,如果您能从中得到更多的想法,我将很高兴。

from sys import argv
from os.path import exists
def bbc(a, b, c, d):
    e = "\nI want to buy {} \nAnd I want to buy {} either \nI even want to get {} \nAll right I'll give you {} books"
    print(e.format(a, b, c, d))

book1, book2, book3, book4, book5 = argv 

a1 = "python book"
b1 = "C books"
c1 = "java books"
d1 = 2

#method1 string
bbc("python books", "C books", "java books", "2")

#method2 variables
bbc(a1, b1, c1, d1)

#method3 number with math
bbc(a1, b1, c1, d1*3)

#method4 string with math
bbc(
"python books" + " either python2.0 or 3.0 is ok", 
"C books" + " or C++ book is ok", 
"java books" + " I mean javascript", 
"2 + 2")

#method5 variables with math
a2 = " either python2.0 or 3.0 is ok"
b2 = " or C++ book is ok"
c2 = " I mean javascript"
bbc(a1 + a2, b1 + b2, c1 + c2, d1 + d1)

#method6 "input" to arguments
d2 = input("num")
bbc(a1, b1, c1, d2)

#method7 "argv" to arguments
bbc(book2, book3, book4, book5)

#method8 "exists" to arguments
txt1 = "Guitar.txt"
txt2 = "Time Travel.txt"
txt3 = "A song for love.txt"
txt4 = "Wonderful Tonight.txt"
bbc(exists(txt1), exists(txt2), exists(txt3), exists(txt4))

#method9 "len" to arguments
I1 = input("name1\t")
I2 = input("name2\t")
I3 = input("name3\t")
I4 = input("name4\t")

W1 = open(I1, "w").write("close")
W2 = open(I2, "w").write("lose")
W3 = open(I3, "w").write("ose")
W4 = open(I4, "w").write("se")

L1 = open(I1).read()
L2 = open(I2).read()
L3 = open(I3).read()
L4 = open(I4).read()
bbc(len(L1), len(L2), len(L3), len(L4))

#method10 "int" to arguments
int1 = int(input("integer 20\t"))
int2 = int(input("binary 10100\t"), 2)
int3 = int(input("octal 24\t"), 8)
int4 = int(float(input("float 20.9\t")))
bbc(int1, int2, int3, int4)

答案 5 :(得分:-1)

导入随机

first = random.randrange(1, 10)  
second = random.randrange(1,10)  
cheese_and_crackers(first, second)