Help please. How to do this ... For example, from the number 1, hash (b option) is taken, from the resulting hash one more hash (x option) is taken, etc. How to change the code so that you can simply set the number of options, for example 10 ( without writing x =, y =, etc.)

import hashlib for _ in xrange(10): b= hashlib.sha256("1").hexdigest() print (b) x=hashlib.sha256(b).hexdigest() print (x) y=hashlib.sha256(x).hexdigest() print(y) 
  • Do you use x twice (in the loop and inside it) - is it intended? If I understand correctly, you can write something like b = "1" and then for _ in range(10): b = hashlib.sha256(b).hexdigest() - ⷶ ⷩ ⷮ ⷪ ⷩ
  • Fixed, so not conceived - Dima

1 answer 1

Do you want to get this? (tenfold application, one object output)

 f = open('text.txt') for line in f: b = line for _ in range(10): b = hashlib.sha256(b).hexdigest() print (b) 

or this: (tenfold application, all intermediate states in the list are at the output)

 b = ["1"] for i in range(1,11): b.append(hashlib.sha256(b[i-1]).hexdigest()) print(b[i]) 
  • Thank you option 1., I did not think it was so easy) I decided that with this option he would take a hash from the same number) - Dima
  • There is another question ... How to make so that the value of b was taken from a file, txt. And if there are 3 lines in the file (for example), this code was applied to each value of the line successively, for example 10 times - Dima
  • please help - Dima
  • Does not work. Writes IndentationError: expected an indented block .... indicating b = f - Dima
  • I edited the answer, adding there reading - MBo