Correction Python

Solved
Arcadi -  
mamiemando Posted messages 33537 Registration date   Status Modérateur Last intervention   -
Bonjour,

I am stuck on a pix exercise. I need to correct the following code so that it works:

The only issue I have identified with my mini level is that I need to add the print command at the end.

indice = "vbpewfdwaz" lettres = "}_v^yk[~jo" i = 0 tutu = '' while i < len(indice): if ord(indice[i]) < 109: tutu=tutu+chr(ord(lettres[i])+10) else: tutu=tutu+chr(ord(lettres[i])-10) i += 1 print(tutu)


If someone could help me it would be nice so that I can understand.

Arcadi

2 réponses

mamiemando Posted messages 33537 Registration date   Status Modérateur Last intervention   7 927
 
Hello,

Since
i
is not incremented, the
while
loop will repeat indefinitely.

Then, I recommend writing your iterables in plural and your iterators in singular, e.g.

for element in elements: ...


Moreover, using indices in this case is not very Pythonic. Unlike C, you can directly iterate over the characters of a string. This will also prevent you from forgetting to increment
i
:-)

If you need to iterate over two iterables simultaneously (which is the case here since you want to advance on
indices
and
letters
simultaneously), you can use the primitives provided by
itertools
or the built-in Python functions: in your case, the one that is suitable is
zip
.

In your case, you could have written something like this:

indices = "vbpewfdwaz" letters = "}_v^yk[~jo" tutu = "" for (indice, lettre) in zip(indices, letters): if ord(indice) < 109: tutu = tutu + chr(ord(lettre) + 10) else: tutu = tutu + chr(ord(lettre) - 10) 


The program can be written even more concisely using the
str.join
function:

indices = "vbpewfdwaz" letters = "}_v^yk[~jo" tutu = "".join( chr(ord(lettre) + 10) if ord(indice) < 109 else chr(ord(lettre) - 10) for (indice, lettre) in zip(indices, letters) )


Good luck!
1
Belinda
 

Hello, I have exactly the same problem, so I copied the answer but it doesn't work. I also tried print "tutu", removing the "S" from "lettres", etc. but absolutely nothing works, on

repl.it when I read my programming I get python3 main.py

HELP !!

0
mamiemando Posted messages 33537 Registration date   Status Modérateur Last intervention   7 927 > Belinda
 

Hello Belinda,

  • The code above works, I just checked. You can do print(tutu) and see that tut indeed contains the string "silhouette".
  • If the problem persists, please open a new discussion thread, reporting the code you executed and copying the exact error message.
  • In general, avoid writing in a resolved discussion thread. If necessary, provide a link to the discussion you are referring to.

Good luck

2
Arcadi
 
Hello Mamiemando,

Thank you very much for your response. I will study everything not just to do but especially to understand. Your complete answer helps me a lot.

Regarding the ethical writing guidelines, this part is really what I was provided with.

Best regards,

Arcadi
0
mamiemando Posted messages 33537 Registration date   Status Modérateur Last intervention   7 927
 
Thank you for your feedback. Regarding programming advice, you'll see over time :-) You can also check the PEP-8 standard if you want to improve.

Good luck!
0