2012-02-12 4 views
0

私はZed ShawのLPTHW本をやっていますが、私はこの余分なクレジット3 questionで立ち往生しています。LPTHW、EX19エクストラクレジット3

エクストラクレジットの割り当て: 少なくとも1つの独自のデザインを作成し、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)'まあまあまあ。 –

+0

ループで実行するのはどうですか?それとも、関数を別の関数に渡しますか? –

+0

@PeterRowellまあ、私は初心者です。あなたの返信に貢献してくれてありがとう、私は変数に関数を代入することができないことを知りませんでした。あなたが "変数"に関係なく他の方法を知っていれば、ありがとう! – Drageek

答えて

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

"私はここのコメントをコメントにまとめています。"それは[**コミュニティウィキ**](http://meta.stackexchange.com/questions/11740/what-are-community-wiki-posts)になります。 – phant0m

-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)