What’s wrong with this python code?

Asked

Viewed 1,862 times

1

import urllib.request
import time

def send_to_twitter():
    import sys
    import tweepy
    CONSUMER_KEY = '1wrnsF5lB8fEWVzRdvlIqdTl'
    CONSUMER_SECRET = 'eTiylDUHLJgGnTCcxzzCtzHXG4OlHrbY6wLvuZUnAEhrokyNA'
    ACCESS_KEY = '2325915097-q2JYaZ3UGeL9Pr95BJC7643NMyETY6x7Bb8T1q'
    ACCESS_SECRET = '8GRq4e9ukVKcC8XjroM3iLKuZYOM2QtFEdCHXG3TXx0z'
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
    api = tweepy.API(auth)
    api.update_status(msg)

def get_price():
    page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices-loyalty.html")
    text = page.read().decode("utf8")
    where = text.find('>$')
    start_of_price = where + 2
    end_of_price = start_of_price + 4
    return(text[start_of_price : end_of_price])

escolha = input("Precisa do valor imediatamente? ")

if escolha == 'y':
    send_to_twitter(get_price())
else:
    if escolha == 'n':
        price = 99.99
        while price > 4.74:
            price = float(get_price())
            time.sleep(3)
        send_to_twitter('BUY!')
    else:
        print("Voce nao escolheu nada")

Error message:

Traceback (most recent call last):
  File "(livro)programa_com_escolha_cafe.py", line 34, in <module>
    send_to_twitter('BUY!')
TypeError: send_to_twitter() takes 0 positional arguments but 1 was given
  • You are giving an argument in a function that receives no argument.

1 answer

7

Your code is producing the following error:

TypeError: send_to_twitter() takes 0 positional arguments but 1 was given

Analyzing the message, we see that this is a TypeError, that tells us that the function send_to_twitter() should receive 0 arguments, but however, you passed 1 argument when writing the term 'BUY!', inside send_to_twitter('BUY!').


At the very beginning of the code, we see that the function send_to_twitter() was created without asking for any argument.

Try changing the section:

def send_to_twitter():
    import sys
    import tweepy
    ...

for

def send_to_twitter(msg):
    import sys
    import tweepy
    ...

Exemplifying...

Wrong:

def imprimirNaTela():
    print msg

Correct:

def imprimirNaTela(msg):
    print msg
  • 1

    +1 hadn’t even noticed this one: send_to_twitter('BUY!')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.