Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何保持Python脚本无限运行(循环)?_Python_Loops_Telegram - Fatal编程技术网

如何保持Python脚本无限运行(循环)?

如何保持Python脚本无限运行(循环)?,python,loops,telegram,Python,Loops,Telegram,我正在构建一个电报机器人,每当我在电报上向机器人发送“/price”命令时,它就会告诉我比特币的美元价格。它可以工作,但是价格不会更新,除非我重新运行Python脚本。 我怎样才能让脚本永远运行,这样我就不需要不断地点击“运行”? 这是我的密码: import requests import telebot # BITCOIN DATA url = 'https://api.coinbase.com/v2/prices/USD/spot?' response = requests.get(u

我正在构建一个电报机器人,每当我在电报上向机器人发送“/price”命令时,它就会告诉我比特币的美元价格。它可以工作,但是价格不会更新,除非我重新运行Python脚本。 我怎样才能让脚本永远运行,这样我就不需要不断地点击“运行”? 这是我的密码:

import requests
import telebot


# BITCOIN DATA
url = 'https://api.coinbase.com/v2/prices/USD/spot?'
response = requests.get(url).json()



# BOT STUFF
bot = telebot.TeleBot("1135809125:AAHHx7sZ5276Kg34VWYDuwHIJB76s5QS9UQ")


@bot.message_handler(commands=['price'])
def send_welcome(message):
    bot.reply_to(message, "The current price of Bitcoin in USD is " + response['data'][0]['amount'])


@bot.message_handler(func=lambda message: True)
def echo_all(message):
    bot.reply_to(message, message.text)


bot.polling()

使用
while
循环:

import requests
import telebot

# BITCOIN DATA
url = 'https://api.coinbase.com/v2/prices/USD/spot?'
response = requests.get(url).json()



# BOT STUFF
bot = telebot.TeleBot("1135809125:AAHHx7sZ5276Kg34VWYDuwHIJB76s5QS9UQ")


@bot.message_handler(commands=['price'])
def send_welcome(message):
    bot.reply_to(message, "The current price of Bitcoin in USD is " + response['data'][0]['amount'])


@bot.message_handler(func=lambda message: True)
def echo_all(message):
    bot.reply_to(message, message.text)

while True:

    bot.polling()

可以尝试使用
itertools

import itertools
for x in itertools.repeat(1):
    bot.polling()
itertools.count()

或者根据,您可以尝试以下方法:

if __name__ == '__main__':
     bot.polling(none_stop=True)

响应应该在
send\u welcome
中,以便在每次发送/price命令时获取当前价格

@bot.message\u处理程序(命令=['price'])
def发送_欢迎(信息):
response=requests.get(url).json()
bot.reply_to(消息,“比特币当前美元价格为”+响应['data'][0]['amount']))

它被称为循环,在许多现有教程中都有介绍。因此,这个问题超出了堆栈溢出的范围。最后一次编辑仍然不起作用:(尽管如此,我真的很感激
if __name__ == '__main__':
     bot.polling(none_stop=True)