Home > Back-end >  Using python to solve for value that meets a condition
Using python to solve for value that meets a condition

Time:11-27

new true value that meets the condition = v

previous true value = vprev

I am trying to look for a v so that hash of str(power(v,2) power(vprev, 3) begins with ee

I tried this

import hashlib
values_list = []# a list where v and prev will be
solved = False
v = 1 # to start looping from 1

while not solved:
    for index, v in enumerate(values_list):
        vprev = values_list[(index - 1)]
    results = str(v**2   vprev**3)
    results_encoded = results.encode()
    results_hashed = hashlib.sha256(results_encoded).hexdigest()
    if results[0:2] == "ee":
        solved = True
        values_list.append(v)
    else: v  = 1

print(values_list)

I'm expecting a list with the first true value but I have failed

CodePudding user response:

Commenting your own code:

# ...
solved = False
v = 1 # to start looping from 1

while solved:
    # This block is never executed: the `while` condition-check fails since the initial state of `solved` is `False`

print(values_list)

you'll probably want to use while not solved: instead

CodePudding user response:

import hashlib
values_list = []# a list where v and prev will be
solved = False
v = 1 # to start looping from 1

while not solved:
    for index, value in enumerate(values_list):
        vprev = values_list[(index - 1)]
    results = str(v**2   vprev**3)
    results_encoded = results.encode()
    results_hashed = hashlib.sha256(results_encoded).hexdigest()
    if results[0:2] == "ee":
        values_list.append(v)
        solved = True
    else: v  = 1

print(values_list)

While loop is working for "true" statment. So you have to change your statments on code. And If you make the statment false then while loop breaks the loop.

  • Related