When writing a telegram bot in python, it gives the error SyntaxError: 'break' outside loop
import random
import telebot
spisok=('автострада','спасибо')
a=random.choice(spisok)
b=(list(a))
n=list(("*"*len(a)))
bot = telebot.TeleBot("1081897647:AAGHBdT1VeS6xVJPAq0yhB_JfvlcAWKOT")
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.send_message(message.chat.id, "Привет,сейчас ты сыграешь в игру виселица")
bot.send_message(message.chat.id, ''.join(n))
bot.send_message(message.chat.id, "Введите одну букву")
def text(message):
s=0
if message.text in b:
for idx, symbol in enumerate(b):
if symbol == message.text:
n[idx] = symbol
if n==b:
bot.send_message(message.chat.id, "Вы выиграли")
bot.send_message(message.chat.id, "Поздравляю")
else:
bot.send_message(message.chat.id, "Данной буквы нет")
s+=1
if s==5:
return
bot.send_message(message.chat.id, ''.join(n))
bot.polling(none_stop=True)
0
1 answers
SyntaxError: 'break' outside loop
See where you have break? As far as I can see, in a single place. Inside the FUNCTION description:
def text(message):
s=0
if message.text in b:
. . .
else:
. . .
if s==5:
break
And where do You see here the CYCLE, within which there must be an operator of break? If You are, in fact, at this point of the program you wish to complete is not the execution of a non-existent cycle, and the function, it is necessary to put - {return !
1
Author: Sergey, 2020-01-26 07:42:09