V
V
vanderhuuy2020-12-26 21:36:45
Python
vanderhuuy, 2020-12-26 21:36:45

How to create a bot in a telegram with a game of bulls and cows (there is a program)?

In general, I puzzled myself with the creation of a bot in a telegram with a game of bulls and cows (there are rules on Wikipedia). In short: the bot guesses a random four-digit number in which the numbers do not repeat. The user, in turn, must guess, after each attempt, finding out the number of matching digits and their positions (bulls) and simply the fact that there is a digit in the desired number (cows). I already wrote the prog itself in python, with it, fortunately, there are no problems. Everything rests on the fact that I need to adapt the finished program to the work of the bot, but this does not work out for me - the usual methods do not work. I am inexperienced and I think that these are the costs of working with
bots

import random
random_numbers_list=[]
amount_inlist=len(random_numbers_list)
while amount_inlist!=4537:
    number=random.randint(1000,10000)
    n1=number%10
    n2=number//10%10
    n3=number//100%10
    n4=number//1000
    if n1!=n2 and n1!=n3 and n1!=n4 and n2!=n3 and n2!=n4 and n3!=n4:
        random_numbers_list.append(number)
    amount_inlist=len(random_numbers_list)
    def f(random_numbers_list):
        n=[]
        for i in random_numbers_list:
            if i not in n:
                n.append(i)
        return n
random_numbers_list=f(random_numbers_list)
random_numbers_list.sort()
our_number=random.choice(random_numbers_list)
s1=our_number%10
s2=our_number//10%10
s3=our_number//100%10
s4=our_number//1000
secretspisok=[s4,s3,s2,s1]
bull=0
cow=0
print(our_number)
while bull<4:
    cow=0 
    bull=0
    attempt=int(input())
    a1=attempt%10
    a2=attempt//10%10
    a3=attempt//100%10
    a4=attempt//1000
    attemptspisok=[a4,a3,a2,a1]
    for a in range(0,4):
        if secretspisok[a]==attemptspisok[a]:
            bull+=1
    for a in range(0,4):
        for b in range (0,4):
            if secretspisok[a]==attemptspisok[b]:
                cow+=1
    print('быков:',bull, 'коров:', int(cow)-int(bull))            
    if bull==4:
        print('Вы победили!Ура!')

The problem starts right away - on the line with the creation of an empty random_numbers_list list. Python simply underlines the line here and throws an invalid syntax error. Then I created a function where I stuffed the part with guessing the number:
def choose_number(random_numbers_list):
    random_numbers_list=[]
    amount_inlist=len(random_numbers_list)
    while amount_inlist!=4537:
        number=random.randint(1000,10000)
        n1=number%10
        n2=number//10%10
        n3=number//100%10
        n4=number//1000
        if n1!=n2 and n1!=n3 and n1!=n4 and n2!=n3 and n2!=n4 and n3!=n4:
            random_numbers_list.append(number)
            amount_inlist=len(random_numbers_list)
        def f(random_numbers_list):
            n=[]
            for i in random_numbers_list:
                if i not in n:
                    n.append(i)
            return n
    random_numbers_list=f(random_numbers_list)
    return(random_numbers_list)
our_number=random.choice(choose_number)

But here another disappointment awaits me - a random number is not taken. If I write like this:
our_number=random.choice(choose_number(random_numbers_list))

then it does not recognize random_numbers_list at all, although in the same program everything works like clockwork. Moreover, even the method with creating a list and equalizing does not work, as I did with the f (random_number_list) function.
Explain what the problem might be or how you can implement getting a random number from the list in a different way. Thanks

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question