furas

SO: how-to-fix-infinite-printing-on-while-statement

Aug 26th, 2019
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://stackoverflow.com/questions/57651384/how-to-fix-infinite-printing-on-while-statement
  2.  
  3. # As I said in comment you forgot `x = x + 1` when you display `" "` so it never move to next char in message
  4. # and it check again and again the same char which is converted to `" "`
  5.  
  6.     import string
  7.  
  8.     letras = list(string.ascii_lowercase)
  9.     message = input("qual sua menssagem codificada?")
  10.     cod = input("qual a numeração da cifra?")
  11.     meslist = list(message)
  12.    
  13.     trans = True
  14.     num = len(meslist)
  15.     x = 0
  16.     while trans:
  17.    
  18.         if num > x:
  19.             if meslist[x] in letras:
  20.                 a = letras.index(meslist[x])
  21.                 print(letras[a + 2])
  22.                 x = x + 1
  23.                 trans = True
  24.             else:
  25.                 print(" ")
  26.                 x = x + 1   # <---
  27.                 trans = True
  28.         elif num == 0:
  29.                 trans = False
  30.  
  31. # But you could write it with `for` loop and then you don't need `x` and `trans`
  32.  
  33. # You also forget that sometimes `letras[a + 2]` may give error `IndexError: list index out of range`
  34. # and then in `caesar cipher` you have to move to beginning `letras[ a+2-len(letras) ]`
  35. # (or alwasy take modulo `letras[ a+2 % len(letras) ]`)
  36.  
  37.     import string
  38.    
  39.     letters = string.ascii_lowercase
  40.    
  41.     message = input("message: ")
  42.     code = int(input("number: "))
  43.    
  44.     for char in message:
  45.         if char in letters:
  46.             position = letters.index(char)
  47.             position += code # position += 2
  48.             if position > len(letters):
  49.                 position -= len(letters)
  50.             print(letters[position], end='') # don't go to new line
  51.         else:
  52.             print(" ", end='') # don't go to new line
  53.            
  54.     print()  # go to new line
RAW Paste Data