Forums

How do I run my python. (Need tips for python- I'm a beginner)

I'm new to python, and even newer to pythonanywhere. I opened a file and only wrote: print("Hello World"). I don't really know how to run it, just to see it work. I've used other things to write and run code. I can't remember what all I've worked with, but I think on sololearn and idk where else. I'm a beginner and would really love any tips.

There are multiple ways of running your Python code on PythonAnywhere (you can run it as a task or web app, or in a notebook -- some of those features require a paid account). But the simplest way -- just to evaluate a code that is in a script, is to run the from a Bash console / in a Python console, or -- if you want to use the in-browser editor -- by using the Run button in the editor.

.

import telebot
from telebot import types

bot = telebot.TeleBot("REDACTED")

@bot.message_handler(commands=['start'])
def send_welcome(message):
    bot.reply_to(message, "Welcome to the Calculator Bot. Send me an arithmetic expression and I'll calculate it for you "
    " created by @amiir_exe")
    send_inline_keyboard(message)

@bot.message_handler(commands=['help'])
def send_help(message):
    bot.reply_to(message, "Available commands:\n"
                          "/add [number1] [number2] - Add two numbers\n"
                          "/subtract [number1] [number2] - Subtract two numbers\n"
                          "/multiply [number1] [number2] - Multiply two numbers\n"
                          "/divide [number1] [number2] - Divide two numbers")

def send_inline_keyboard(message):
    keyboard = types.InlineKeyboardMarkup()
    button1 = types.InlineKeyboardButton("help", callback_data="help")   
    button2 = types.InlineKeyboardButton("Powered", callback_data="Powered")
    button3 = types.InlineKeyboardButton("channal", callback_data="channal")
    keyboard.add(button1, button2, button3)

    bot.send_message(message.chat.id, "Choose one obtion:", reply_markup=keyboard)


@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
    if call.data == "help":
        bot.send_message(call.message.chat.id, "Available commands:\n"
                          "/add [number1] [number2] - Add two numbers\n"
                          "/subtract [number1] [number2] - Subtract two numbers\n"
                          "/multiply [number1] [number2] - Multiply two numbers\n"
                          "/divide [number1] [number2] - Divide two numbers")
    elif call.data == "Powered":
        bot.send_message(call.message.chat.id, "Powered by @amiir_exe")
    elif call.data == "channal":
        bot.send_message(call.message.chat.id, "channal link -> https://t.me/+FWz0h6fN0BtjMmFk")

@bot.message_handler(commands=['add'])
def add_numbers(message):
    args = message.text.split()[1:]
    
    if len(args) != 2:
        bot.reply_to(message, "Invalid number of arguments. Usage: /add [number1] [number2]")
    else:
        try:
            result = float(args[0]) + float(args[1])
            bot.reply_to(message, f"Result: {result}")
        except ValueError:
            bot.reply_to(message, "Invalid number format. Usage: /add [number1] [number2]")

@bot.message_handler(commands=['subtract'])
def subtract_numbers(message):
    args = message.text.split()[1:]
    
    if len(args) != 2:
        bot.reply_to(message, "Invalid number of arguments. Usage: /subtract [number1] [number2]")
    else:
        try:
            result = float(args[0]) - float(args[1])
            bot.reply_to(message, f"Result: {result}")
        except ValueError:
            bot.reply_to(message, "Invalid number format. Usage: /subtract [number1] [number2]")

@bot.message_handler(commands=['multiply'])
def multiply_numbers(message):
    args = message.text.split()[1:]
    
    if len(args) != 2:
        bot.reply_to(message, "Invalid number of arguments. Usage: /multiply [number1] [number2]")
    else:
        try:
            result = float(args[0]) * float(args[1])
            bot.reply_to(message, f"Result: {result}")
        except ValueError:
            bot.reply_to(message, "Invalid number format. Usage: /multiply [number1] [number2]")

@bot.message_handler(commands=['divide'])
def divide_numbers(message):
    args = message.text.split()[1:]
    
    if len(args) != 2:
        bot.reply_to(message, "Invalid number of arguments. Usage: /divide [number1] [number2]")
    else:
        try:
            result = float(args[0]) / float(args[1])
            bot.reply_to(message, f"Result: {result}")
        except ZeroDivisionError:
            bot.reply_to(message, "Cannot divide by zero.")
        except ValueError:
            bot.reply_to(message, "Invalid number format. Usage: /divide [number1] [number2]")
print("done")
bot.polling()

[edit by admin: formatting, also redacted Telegram token]

Are you having problems when you run that code? BTW, you shouldn't post code with secrets in these forums -- I noticed that your original post had the actual Telegram token at line 4, and that's something you should not share with other people, because anyone that has it can run a bot that will act as yours. For security, I've replaced it with the word "REDACTED".