2011-06-21 18 views
0

以下は、ウェブサイトの価格をチェックしてTwitterに送信するコードです。ご覧のとおり、22行目では、2番目の関数(Twitterにストリームする)の引数として、最初の関数(価格を取得する)を渡します。私がこれを実行すると、 "TypeError:send_to_twitter()は引数をとりません(1が指定されています)"というエラーメッセージが表示され続けます。なぜそれが議論をとらないのか分かりません。何か案が?関数は引数を取らない

import urllib.request 
import time 

def get_price(): 
    page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")#get price from website 
    text = page.read().decode("utf8") 
    where = text.find('>$') 
    start_of_price = where + 2 
    end_of_price = start_of_price + 4 
    return float(text[start_of_price:end_of_price]) 


def send_to_twitter(): 
    password_manager = urllib.request.HTTPPasswordMgr() 
    password_manager.add_password('Twitter API','http://twitter.com/statuses','eyemademusic','selfishgene') 
    http_handler = urllib.request.HTTPBasicAuthHandler(password_manager) 
    page_opener = urllib.request.build_opener(http_handler) 
    urllib.request.install_opener(page_opener) 
    params = urllib.parse.urlencode({'status':msg}) 
    resp = urllib.request.urlopen('http://twitter.com/statuses/update.json', params) 
    resp.read 

price_now = input('Would you like to check the price? Y/N') 
if price_now == 'Y': 
    send_to_twitter(get_price()) 
else: 
    price = 99.99 
    while price > 4.74: 
     time.sleep(900) 
     price = get_price 
    send_to_twitter('Buy!') 

答えて

5
def send_to_twitter(name_of_the_argument_you_want): 
3

def send_to_twitter():resp.read()price = get_priceでなければなりません。このため、price = get_price()

3

でなければなりませんdef send_to_twitter(msg):

resp.read次のようになります。

def send_to_twitter(): 
    ... 

定義引数がゼロの関数。これについて少し考えてみてください。あなたが望む議論をどのように参照しますか?関数の中にはどのような名前がありますか?関数名の後のかっこの中で、関数が取るすべての引数の名前をリストする必要があります。あなたがこれを持っている。また

、:あなたが実際にsend_to_twitterに引数として機能get_priceを渡していない

send_to_twitter(get_price()) 

、あなたはget_priceを呼び出し、その結果を渡しています。関数を渡したい場合は、かっこではなく関数名を使用するだけで済みます。

send_to_twitter(get_price) 
関連する問題