jeudi 3 juin 2021

Why does my algorithm stop after a while? [closed]

I wrote an Algorithm to guess a number the user inputs:

import random

def innum(x):
    innumber = ""
    for y in range(x):
        innumber += str(y)
    return innumber

def ml():
    wrong = ""
    while True:
        guess = start
        action = random.choice(["+","-"])
        if action == "+":
            guess += random.randint(0,1000)
        if action == "-":
            guess -= random.randint(0,1000)
        if "-" not in str(guess):
            if str(guess) not in wrong:
                if guess == answer:
                    print("Correct: " + str(answer))
                    break
                else:
                    print("Wrong:" + str(guess))
                    wrong += str(guess)
start = random.randint(0,1000)
answer = input("What number to go to from " + str(start) + ". Has to be in range 2000.")
if answer in innum(2000):
        ml()
else:
     print("Number not in range 2000.")

But after a while it just stops I ran it multiple times and it keeps stopping and never gets a answer. I read the program multiple times and I still don't know why it stops.




Aucun commentaire:

Enregistrer un commentaire