0

Can someone help me with this code please???

My code works perfectly EXCEPT for one thing. When I enter a letter it translates it to binary and then asks me for entering another letter. But when I enter my second letter (or3ª, or 4ª, or 5ª, and more), it gives me the same result that before. If I want it to tell me another letter I have to close the entire program and re-open it. I tried with a lot of things. Sorry, the code's instructions are in Spanish. Here is the code: print("Python Translator") print("""Divide tus palabras a letras. Despues copia y pega en algun lugar y continua con las demas letras. Favor de SOLO escribir letras Mayusculas. Si quieres escribir un espacio escribe ESPACIO. Para escribir otra letra cierra y abre el traductor, ya que si no escribira lo mismo. Gracias. Disfruta el taductor :)""") I1 = "Enter one letter: " I2 = "Enter one letter: " I3 = "Enter one letter: " I4 = "Enter one letter: " I5 = "Enter one letter: " I6 = "Enter one letter: " I7 = "Enter one letter: " I8 = "Enter one letter: " I9 = "Enter one letter: " I10 = "Enter one letter: " I11 = "Enter one letter: " I12 = "Enter one letter: " I13 = "Enter one letter: " I14 = "Enter one letter: " I15 = "Enter one letter: " I16 = "Enter one letter: " I17 = "Enter one letter: " I18 = "Enter one letter: " I19 = "Enter one letter: " I20 = "Enter one letter: " I21 = "Enter one letter: " I22 = "Enter one letter: " I23 = "Enter one letter: " I24 = "Enter one letter: " I25 = "Enter one letter: " I26 = "Enter one letter: " I27 = "Enter one letter: " I28 = "Enter one letter: " I29 = "Enter one letter: " A1 = "Another Letter?: " pr1 = input(I1) while pr1 == "A": print("01000001, ") pr2 = input(I2) while pr1 == "B": print("01000010, ") pr3 = input(I3) while pr1 == "C": print("01000011, ") pr4 = input(I4) while pr1 == "D": print("01000100, ") pr5 = input(I5) while pr1 == "E": print("01000101, ") pr6 = input(I6) while pr1 == "F": print("01000110, ") pr7 = input(I7) while pr

15th Sep 2018, 4:16 AM
Octavio Aguayo
Octavio Aguayo - avatar
10 odpowiedzi
+ 3
Whoa! First of all, no need to have 30 I1 - I29 variables that are all the same. You can use one variable more than once! Secondly, use a dictionary for the binary codes! letters = { 'A' : '01000001', 'B' : '01000010', ... } The reason why you have to force-close the program is because if you enter say "A", the `while pr1 == "A"` will always be true and so you just keep looping forever. Your code should probably look something like while true: x = input("Enter letter pls") print( letters[x] ) And then some checks to see if you want to read another letter or not.
15th Sep 2018, 4:41 AM
Schindlabua
Schindlabua - avatar
+ 3
Dictionaries have keys and values. You put in the key, you get out the value. Say you have letters = { 'A' : '01000001', 'B' : '01000010', .... } then `letters["A"]` will give you "01000001".
15th Sep 2018, 4:47 AM
Schindlabua
Schindlabua - avatar
+ 3
see my first post: You save the input in any variable you want, then you look it up in the dictionary. x = input("Letter please") print( letters[x] )
15th Sep 2018, 4:55 AM
Schindlabua
Schindlabua - avatar
+ 3
Here is the sample code for A and B: letters = { 'A' : '01000001', 'B' : '01000010' } x = input("Letter please: ") print( letters[x] )
15th Sep 2018, 5:00 AM
Schindlabua
Schindlabua - avatar
+ 2
Anyway I need to get going, so one last thing, maybe it helps: Try running through the program in your head. We start with x = input("Letter please:") print( letters[x] ) Then the user inputs, say, "A". The program becomes x = "A" print( letters[x] ) Next, we can replace every x with "A", because that's what variables do. The program becomes simply print( letters["A"] ) And finally we look up the value of "A" in the dictionary. print("01000001") And that's it!
15th Sep 2018, 5:13 AM
Schindlabua
Schindlabua - avatar
+ 1
What the code makes is that the user writes using an “input”. So it is interactive. But thankyou for your help. I’ll try to figure out what you are saying
15th Sep 2018, 5:36 AM
Octavio Aguayo
Octavio Aguayo - avatar
0
Thankyou very much, but... how can I call a specific part of a list?
15th Sep 2018, 4:44 AM
Octavio Aguayo
Octavio Aguayo - avatar
0
And what do I write to call that Insay letters, A = Input or what?? please help
15th Sep 2018, 4:53 AM
Octavio Aguayo
Octavio Aguayo - avatar
0
:)
15th Sep 2018, 4:54 AM
Octavio Aguayo
Octavio Aguayo - avatar
0
I’ m not understanding... Can you please make a sapmple code. With only A and B if you want. Only for me to get an idea of what you are saying
15th Sep 2018, 4:57 AM
Octavio Aguayo
Octavio Aguayo - avatar