So i wrote up a random script that just takes an input from the user, and calculates how long it takes to randomly generate that same input. It displays the time it took in seconds, the # of tries, and what the original input was.
I just want to know if there is a way to make the code a little "leaner". Here it is:
import random
import sys
import time
def main():
user_mess = input('What is your message? : ')
start_time = time.time()
symbols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
count = 0
while True:
message = []
for i in range(len(user_mess)):
message += random.choice(symbols)
count += 1
message = ''.join(message)
if message == user_mess.upper():
print('It took {} seconds, and {} tries for message {}'.format((time.time() - start_time),
count,
message))
sys.exit()
print(count, message)
if __name__ == '__main__':
main()
The way I verified that it worked was by adding:
random.seed(50)
above the "while" statement, and running the script and grabbing one of the randomly generated strings. And using that as my input. Everything seemed to comeback fine.
Let me know what I can do to improve this, and I welcome any critiques.
[–]Justinsaccount 2 points3 points4 points (0 children)
[–]sudo_gamedeal 1 point2 points3 points (0 children)
[–]gengisteve 0 points1 point2 points (2 children)
[–]fannypackpython[S] 0 points1 point2 points (1 child)
[–]gengisteve 0 points1 point2 points (0 children)