Telegram 'guess the number' 男孩每次收到用户的新猜测时都会更改密码
Telegram 'guess the number' bot is changing the secret number every time it recives a new guess from the user
我正在制作一个 'guess the number' 电报机器人,它应该会给出 0 到 100 之间的整数。当用户尝试猜测数字时,机器人应该给他们提示,显示他们的猜测是否正确小于或大于秘密数字。但它似乎每次收到用户的新猜测时都会更改密码。
这是完整的代码
import random
import telebot
from telebot.types import Message
bot = telebot.TeleBot("-token-")
@bot.message_handler(commands = ('start'))
def guess_number():
a = random.randint(0,100)
a = guess_number.variable
def send_welcome(message):
bot.send_message(message.chat.id, "you entered a guessing game, traveller. Guess a number from 0 to 100, or be eaten by a dragon.")
@bot.message_handler(func=lambda message: True)
def checking(message):
try:
user_message = int(message.text)
if user_message > guess_number.variable:
bot.send_message(message.chat.id, "your number is greater than mine")
elif user_message < guess_number.variable:
bot.send_message(message.chat.id, "your number is smaller than mine")
else:
bot.send_message(message.chat.id, "yep, that's the right number")
except ValueError:
bot.send_message(message.chat.id, "you must enter an integer")
bot.polling()
a = random.randint(0,100) a = guess_number.variable
它还说变量 'a',
这是秘密号码的存储,在 guess_number 函数中是遥不可及的。
我认为变量应该放在其他地方,但不能
找出确切位置。
多人聊天
如果您希望它适用于多个用户,您将需要某种数据库来存储每次聊天的号码。字典可以作为一个小例子。
numbers = dict()
@bot.message_handler(commands=('start'))
def guess_number(message):
numbers[message.chat.id] = random.randint(0, 100)
@bot.message_handler(func=lambda message: True)
def checking(message):
try:
user_message = int(message.text)
if user_message > numbers[message.chat.id]:
bot.send_message(message.chat.id, "your number is greater than mine")
elif user_message < numbers[message.chat.id]:
bot.send_message(message.chat.id, "your number is smaller than mine")
else:
bot.send_message(message.chat.id, "yep, that's the right number")
except ValueError:
bot.send_message(message.chat.id, "you must enter an integer")
except (TypeError, KeyError):
bot.send_message(message.chat.id, "there's no number to guess! say /start to start")
单次聊天
如果您只需要此机器人进行一次聊天,您可以选择使用单个全局变量来存储密码。然后使用 global
关键字使其在函数内可访问。
the_number = None
@bot.message_handler(commands=('start'))
def guess_number(message):
global the_number
the_number = random.randint(0, 100)
@bot.message_handler(func=lambda message: True)
def checking(message):
global the_number
try:
user_message = int(message.text)
if user_message > the_number:
bot.send_message(message.chat.id,"your number is greater than mine")
elif user_message < the_number:
bot.send_message(message.chat.id,"your number is smaller than mine")
else:
bot.send_message(message.chat.id, "yep, that's the right number")
except ValueError:
bot.send_message(message.chat.id, "you must enter an integer")
无论哪种情况,当机器人关闭时,号码都会丢失。
选择合适的 keep this in mind
我正在制作一个 'guess the number' 电报机器人,它应该会给出 0 到 100 之间的整数。当用户尝试猜测数字时,机器人应该给他们提示,显示他们的猜测是否正确小于或大于秘密数字。但它似乎每次收到用户的新猜测时都会更改密码。
这是完整的代码
import random
import telebot
from telebot.types import Message
bot = telebot.TeleBot("-token-")
@bot.message_handler(commands = ('start'))
def guess_number():
a = random.randint(0,100)
a = guess_number.variable
def send_welcome(message):
bot.send_message(message.chat.id, "you entered a guessing game, traveller. Guess a number from 0 to 100, or be eaten by a dragon.")
@bot.message_handler(func=lambda message: True)
def checking(message):
try:
user_message = int(message.text)
if user_message > guess_number.variable:
bot.send_message(message.chat.id, "your number is greater than mine")
elif user_message < guess_number.variable:
bot.send_message(message.chat.id, "your number is smaller than mine")
else:
bot.send_message(message.chat.id, "yep, that's the right number")
except ValueError:
bot.send_message(message.chat.id, "you must enter an integer")
bot.polling()
a = random.randint(0,100) a = guess_number.variable
它还说变量 'a',
这是秘密号码的存储,在 guess_number 函数中是遥不可及的。
我认为变量应该放在其他地方,但不能
找出确切位置。
多人聊天
如果您希望它适用于多个用户,您将需要某种数据库来存储每次聊天的号码。字典可以作为一个小例子。
numbers = dict()
@bot.message_handler(commands=('start'))
def guess_number(message):
numbers[message.chat.id] = random.randint(0, 100)
@bot.message_handler(func=lambda message: True)
def checking(message):
try:
user_message = int(message.text)
if user_message > numbers[message.chat.id]:
bot.send_message(message.chat.id, "your number is greater than mine")
elif user_message < numbers[message.chat.id]:
bot.send_message(message.chat.id, "your number is smaller than mine")
else:
bot.send_message(message.chat.id, "yep, that's the right number")
except ValueError:
bot.send_message(message.chat.id, "you must enter an integer")
except (TypeError, KeyError):
bot.send_message(message.chat.id, "there's no number to guess! say /start to start")
单次聊天
如果您只需要此机器人进行一次聊天,您可以选择使用单个全局变量来存储密码。然后使用 global
关键字使其在函数内可访问。
the_number = None
@bot.message_handler(commands=('start'))
def guess_number(message):
global the_number
the_number = random.randint(0, 100)
@bot.message_handler(func=lambda message: True)
def checking(message):
global the_number
try:
user_message = int(message.text)
if user_message > the_number:
bot.send_message(message.chat.id,"your number is greater than mine")
elif user_message < the_number:
bot.send_message(message.chat.id,"your number is smaller than mine")
else:
bot.send_message(message.chat.id, "yep, that's the right number")
except ValueError:
bot.send_message(message.chat.id, "you must enter an integer")
无论哪种情况,当机器人关闭时,号码都会丢失。 选择合适的 keep this in mind