0

i have tried but can't seem to find my mistake in my code. My code is suppose to switch all the alphabetic characters (like a/aa/A/AA) and do nothing with the rest but when i run the code it doesn't give an error yet do what i want.

Could anyone tell me what i have done wrong or have forgotten?

letter = input("type something")
shift = int(input("type how many shifts"))
if letter in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:
    a = ord(letter) + shift
    b = chr(a)
    print(b)
else: 
    print(letter)

EDIT: thanks for the == replacement for in! Does someone know why using more than one character in letter gives the same print?(Desired output: when i put in abc and 1 i want it to print bcd)

Omar
  • 15
  • 1
  • 6
  • 1
    Well what *does* it do? Give a [mcve]. – jonrsharpe Oct 01 '16 at 20:49
  • If you want it to make you a margarita you're going to need to try a different approach. If you're not trying to make margaritas it would help if you explained your *desired output*. – Wayne Werner Oct 01 '16 at 20:51
  • so sorry forgot to do it – Omar Oct 01 '16 at 20:52
  • **Desired output**. – Wayne Werner Oct 01 '16 at 20:52
  • Use `isalpha()` to check for letters, Check this: http://stackoverflow.com/questions/15558392/how-to-check-if-character-in-string-is-a-letter-python – Saeid Oct 01 '16 at 20:54
  • Give an simple input/output example will be the best way to give a vision of your problem – RandomEli Oct 01 '16 at 20:54
  • `if letter in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:` – George Bou Oct 01 '16 at 20:55
  • is there another way than using islower and isupper? – Omar Oct 01 '16 at 20:56
  • Desired output means you should put something like "*When I put in 'frobnosticate' and 2, I expect that it will output `fizz buzz`, but it prints `crazy time` instead*." – Wayne Werner Oct 01 '16 at 21:01
  • Done, thanks i'm new to this.... – Omar Oct 01 '16 at 21:05

4 Answers4

1
#a is the string you want to encrypt, b is the number you shift by
a,b = input().split()
x = int(b)
#code to make sure that the shift is less than 26
for i in range(x):
  if x > 26:
    x = x - 26
  else:
    break
#driver code
print(''.join(map(chr,([ord(c)+x for c in a]))))
Forrest
  • 11
  • 1
0

I suppose you want to shift the letters so if the input letter is 'a' and shift is 3, then the output should be 'd'.

In that case replace

if letter == ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:

with

if letter in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']:

Or better yet as Tempux suggested you can use

if letter.isalpha()

If you want to shift multple letters you need to loop across each character. Try the following code for multiple letters

letter = input("type something")
shift = int(input("type how many shifts"))
s = ""
for l in letter:
    if l.isalpha():
        a = ord(l) + shift
        s += chr(a)
    else: 
        s += l

print(s)
shubham003
  • 703
  • 2
  • 9
  • 20
0

You compare letter with list, but i think you want to check for contain letter in list, so you should just replace == to in

AyumuKasuga
  • 189
  • 1
  • 8
  • thanks a lot! like i mentioned on shubbam103 i do have another problem still where multiple letters don't work.... – Omar Oct 01 '16 at 21:01
0

From the looks of it, I'd say you're more after something like this:

import string
text = input("type something> ")
shift = int(input("enter number of shifts> "))
for letter in text:
    index = ord(letter) - ord('a') + shift
    print(string.ascii_letters[index % len(string.ascii_letters)])
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147