Python 在Bot电报中转换美元和欧元
我正在学习Python为电报创建一个简单的机器人,但我有一个问题。我创建了两个文件exchange.py和BotHtmlTelegram.py exchange.py:Python 在Bot电报中转换美元和欧元,python,python-3.x,telegram,telegram-bot,Python,Python 3.x,Telegram,Telegram Bot,我正在学习Python为电报创建一个简单的机器人,但我有一个问题。我创建了两个文件exchange.py和BotHtmlTelegram.py exchange.py: EXCHANGE=1.125 def from_usd_to_eur(usd): return usd/EXCHANGE def from_eur_to_usd(eur): return EXCHANGE*eur 这个BotHtmlTelegram.py: from telegram.ext import
EXCHANGE=1.125
def from_usd_to_eur(usd):
return usd/EXCHANGE
def from_eur_to_usd(eur):
return EXCHANGE*eur
这个BotHtmlTelegram.py:
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import exchange
TOKEN="My_Token"
def extract_number(text):
return text.split()[1].strip()
def convert_usd(update, context):
usd=float(extract_number(update.message.text))
eur=exchange.from_usd_to_eur(usd)
print(f'Eseguita conversione da {usd} USD a {eur} EUR')
update.message.reply_text(f'{eur} EUR')
def convert_eur(update, context):
eur=float(extract_number(update.message.text))
usd=exchange.from_eur_to_usd(eur)
print(f'Eseguita conversione da {eur} EUR a {usd} USD')
update.message.reply_text(f'{usd} USD')
def main():
upd= Updater("My_Token", use_context=True)
disp=upd.dispatcher
disp.add_handler(CommandHandler("usd", convert_usd))
disp.add_handler(CommandHandler("eur", convert_eur))
upd.start_polling()
upd.idle()
if __name__=='__main__':
main()
在我的Bot电报中,我设置了/eur和/usd命令-如果我写/eur 100,结果就可以了,但是当我写/usr 100时,什么都没有发生-
我做错了什么
非常感谢,伙计
- 谢谢各位,你们的答案将是我学习的下一步。目前这个小程序正在运行。我运行了第二次,现在/usd(感谢@nordmanden)工作正常了最好使用api