mardi 30 juin 2020

How do I make count the number of random guesses it takes to guess a name without repetitions?

From someone else's question that got deleted, I finally understood what he wanted.

Basically he wanted to count the number of unique guesses to get the correct name.

import time
import random
chars = string.ascii_letters + string.digits + '!@#$£%^&*()[]{}.,~-_\\'
name = input("what is the name you want guessed? ")
start_time = time.time()
passwordlen = len(password)
nameguess = ''.join(random.choice(chars) for i in range(namelen))
x = 1
while nameguess != name or x != 10000000:
    x += 1
    nameguess = ''.join(random.choice(chars) for i in range(namelen))
    print("guess: " + nameguess + " attempt: " + str(x))
if nameguess == name:
    print(f"it only took {x} times")
elif x == 10000000:
    print("i give up")

my first idea was to write it into a file, but i came into a logically problem, how do i make it reset the file everytime the code runs, then append to the file for every guess in that run, and how do I make it after it checks if the current guess has already been guessed, to check the new guess, I don't know if this makes sense

How do I make it reset the file for every time I hit 'run' on the code but append to the file for every guess

And How do I make it go back in code, so I want it to go:

(clearfile)
while nameguess != name
nameguess = ''.join(random.choice(chars) for i in range(namelen))  <------      
with open("guesses.txt", 'r') as guess:                                   |
    if nameguess in guess.read():                                         |
       --------------------------------------------------------------------
    elif nameguess not in guess.read():
        (appened name to list)
       continue

How do I make it not guess the same thing twice is the bottom line, I apologise that it was unclear




Aucun commentaire:

Enregistrer un commentaire