简体   繁体   English

在循环中连续调用函数并输出不同的结果

[英]Calling a function continuously within a loop and printing different results

I am creating a small program that pulls quotes of a certain artist and then posts a random line from a random song of theirs onto Twitter. 我正在创建一个小程序,该程序提取某个艺术家的报价,然后将其随机歌曲中的随机行发布到Twitter。 I have so far managed to pull the lyrics, get a random line from a random song but it always posts the same lyric. 到目前为止,我设法提取了歌词,从随机歌曲中获得了随机歌词,但它总是发布相同的歌词。 I understand why this is happening as it isn't only looping through the same output earlier on. 我了解为什么会发生这种情况,因为它不仅在更早的时间内循环通过相同的输出。 Could you advise how I can get the loop to pull a new song and then a new random lyric each time it loops round? 您能建议我如何使循环循环播放一首新歌,然后在每次循环时循环播放新的随机歌词吗? I have tried to call the 'lyricsimport' function within the loop to no avail 我试图在循环内调用'lyricsimport'函数无济于事

I'm quite new to Python please feedback any improvements if necessary - thank you in advance 我对Python很陌生,请根据需要反馈任何改进-预先感谢您

Here is my code so far... 到目前为止,这是我的代码...

# Import Twitter credentials from credentials.py
import random
from tswift import Artist
import tweepy
from time import sleep
from credentials import *

# Access and authorize our Twitter credentials from credentials.py
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

#get lyrics from tswift and save to text file
def lyricsimport():
    tswift = Artist('Frank Ocean')
    song = random.choice(tswift.songs)
    savefile = open('ocean.txt', 'w')
    savefile.write(song.format())
    savefile.close()


# Open text file ocean.txt (or your chosen file) for reading and select random lyric
with open('ocean.txt') as f:
    my_file = f.readlines()
    file_lines = random.choice(my_file)

# Tweet a line every 10 seconds (will change post-testing)
def tweet():
    # Create a for loop to iterate over file_lines
    for line in file_lines:
        try:
            print(file_lines)
            #if statement to ensure that blank lines are skipped
            if line != '\n':
                api.update_status(file_lines)
                sleep(10)
            else:
                pass
        except tweepy.TweepError as e:
            print(e.reason)
            sleep(2)
tweet()

my_file is all the lines in the file, right? my_file是文件中的所有行,对吗? and so file_lines is a random.choice from those, and random.choice only returns a single value... This should be visible by print(file_lines) - should be just a single line. 因此file_lines是这些中的random.choice ,而random.choice仅返回单个值...这应该由print(file_lines)可见-应该只是一行。

How about you replace 你怎么替换

    api.update_status(file_lines)

in your loop with 在你的循环中

    api.update_status(random.choice(my_file))

?

The file opening should look like this: 文件打开应如下所示:

with open('ocean.txt') as f:
    my_file = f.readlines()

And let's see if that helps :) 让我们看看是否有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM