2012-02-12 63 views
0

我正在做Zed Shaw的LPTHW的书,但我被困在这额外的信贷3 question,发现这里没有任何问题,因此我决定做一个帐户。LPTHW,EX19额外信贷3

额外功劳分配: 至少写一个自己设计的功能,并运行它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) 

那么,有什么其他的方式来运行你的脚本里面的函数吗?您能否以初学者的详细方式帮助我,包括实际的代码,以便我可以尝试并理解它?

+0

您可以随时将函数分配给一个变量,然后以这种方式调用它。例如。 “foo = cheese_and_crackers; foo(10,20)'''''''''''''确实是一种微不足道的东西 – 2012-02-12 23:29:02

+0

如何在一个循环中运行它?或者将该函数传递给另一个函数,它会调用它? – 2012-02-13 00:05:25

+0

@PeterRowell嗯,我是一个初学者,我只是想找出更多的方法来运行函数,感谢您的回复,我不知道我可以将函数赋值给变量,如果您知道其他方式,而不管它们的“编程美学“请写下来,谢谢! – Drageek 2012-02-13 00:17:38

回答

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?'))) 
+0

“我在这里将评论整合成一个答案。”这使得[**社区wiki **](http://meta.stackexchange.com/questions/11740/what-are-community-wiki-posts)。 – phant0m 2013-01-26 19:00:49

-1

进口随机

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

我可以添加一些方法:

#the sum function 
def displaySum(a,b): 
    sum = a + b 
    print "The sum is: %d" % sum 

print "#1:" 
displaySum(5,10)    #1 


print "#2:" 
x=3 
k=7        #2 
displaySum(x,k) 

print "#3:" 
displaySum(5+5, 10+10)   #3 

print "#4:" 
displaySum(x+5,k+10)   #4 

print "#5:" 
var1 = int(raw_input("p1: ")) #5 
var2 = int(raw_input("p2: ")) 
displaySum(var1,var2) 


def passValue():    #6 
    x1 = 100 
    x2 = 200 
    displaySum(x1,x2) 

print "#6:" 
passValue() 
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:')) 
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)