Why the the code puts a / every single time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why the the code puts a / every single time?

Iwould like to do a simple script that translate a letter into a number and, when the number has the same digit of the previous to put a /. Code: def encrypting() : if TOencrypt[count] == "A": if TOencrypt[count-1] == "A" and count > 0: print("/", end = "") print ("2") # same for every letter with elif. TOencrypt = str(input("")) TOencrypt = TOencrypt.upper() Print("final phrase is:") for a in range(strlen): encrypting() count +=1 The code return always a /

20th Jul 2017, 7:08 PM
Hymn
Hymn - avatar
2 Answers
0
Hey E, I am not exactly sure what your code is supposed to do so I can't show you an example, however, I notice you are trying to pass variables back and forth in/out of a function. This doesn't work because a function's variables are not accessible globally. You should be passing a variable into your function and then returning the new value that you manipulated. While you can make a variable global, it isn't recommended unless absolutely necessary. def myfunc(car): return car + " goes fast" car = "BMW" nwstring = myfunc(car) Hope that helps! ~Jim
20th Jul 2017, 9:44 PM
Jim Tully
Jim Tully - avatar
0
Hey Jim, What I want to do is create a code that read a phrase and traslate every letters into a specific number(keep in mind an old phone keyboard) . For example : "see" - - > "777733/33". I'm working with capital letters because it's easier. So, I wrote a for function that read every single character and goes to encrypt() to match them with what I would like to print out. The problem is that it returns a / every single time (/7777/33/33/) instead of only when 2 near characters contain same numbers. That's what the "if in the if" in encrypt() is trying to do. The count variable should prevent the code to print a / at the beginning but it doesn't work. Maybe it has something to do with the elif(for every letter in encrypt()) or the final else - - - >else: print TOencrypt[count]. Thanks a lot for the help you're giving!
21st Jul 2017, 12:31 AM
Hymn
Hymn - avatar