0

I have a little code that takes a user input and encrypts it. It's not a hash formula but its just a simple enigma code. the code simplified is:

def Hash (string):
    for x in range(0, len(string)):
        if x == 0:
            HashC = str(ord(string[x:x+1]))
        else:
            HashC = HashC+str(ord(string[x:x+1]))
            print(HashC)
    U = HashC

U = input("What do you want to hash? ")
Hash(U)
print(U)

The output with the print so show me whats going on in the conversion.

>    What do you want to hash? Test
>    84101
>    84101115
>    84101115116
>    Test

What I want it to Output

>    What do you want to hash? Test
>    84101
>    84101115
>    84101115116
>    84101115116

Because I want the code to change the string into the "encrypted" code.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • What is wrong? You haven't said what you're expecting to see that differs from what you got. I suspect you've got an issue expecting C++ style pass-by-reference semantics (`str` are immutable, and no change in `Hash` will change the variable `U` outside of `Hash`), but that's just a guess at what you think is going wrong. – ShadowRanger Feb 17 '17 at 04:03
  • 2
    `return HashC` instead of `U = HashC` and `U = Hash(U)` instead of `Hash(U)`. As pointed above, strings in Python are immutable. – vendaTrout Feb 17 '17 at 04:07
  • @vendaTrout thank you do you mind creating the answer? Or do you just want me to delete this question? – Keithwayne Feb 17 '17 at 04:08
  • Also, you can take a look at [this](http://stackoverflow.com/a/16865251/767632) answer. – yeputons Feb 17 '17 at 04:10
  • @yeputons this doesn't help with my formula returning the coded answer – Keithwayne Feb 17 '17 at 04:12
  • Also, the assignment `U = HashC` will create a function-local variable called `U` rather than assigning to the global namespace. To fix that, even though it is a bad idea, add `globals U` to the top of your function. – Mad Physicist Feb 17 '17 at 04:26

2 Answers2

2

U in Hash is a local variable, not the global one.

Add global U above U = HashC may solve the case.

But it is a good practice to not modify global variable, as it creates side effects, and it often leads to some kind of bug. So return HashC in Hash and U = Hash(U) instead.

delta
  • 3,778
  • 15
  • 22
1

The scope of the variable U lies outside the function. When you assign U = HashC in the function block, the block assigns it to U but as the function block exits, U is restored to its value of input() in this case Test. This is like pass by value in C, only here U is visible to the function block without explicitly passing to it. Also, you have to store this value in U as strings are immutable, although as such you did not make a change to string

Therefore, you should do something like :

def Hash (string):
    for x in range(0, len(string)):
    if x == 0:
        HashC = str(ord(string[x:x+1]))
    else:
        HashC = HashC+str(ord(string[x:x+1]))
        print(HashC)
    return HashC

U = input("What do you want to hash? ")
U = Hash(U)
print(U)
vendaTrout
  • 146
  • 7
  • Not sure if you were implying this, but the line `U=HashC` in the function actually makes the value of `U` from outside completely invisible within the function. Once you make that assignment, `U` becomes a local variable and no global lookup will be done on it at all. Attempting to reference it in the function before the assignment will cause a `NameError`. – Mad Physicist Feb 17 '17 at 04:31
  • Nope, function can still see `U`. For instance, just define `a = 1`. `def x(): print(a)`. This will print `1`. Correct me if I have not understood your comment properly. – vendaTrout Feb 17 '17 at 04:54
  • `def x(): print(a); a=2` will raise a NameError even if you assign `a=1` globally as you did. Doing `a=2` *anywhere* within the function makes `a` a local variable throughout the entire function – Mad Physicist Feb 17 '17 at 05:47