Try this:
#!/usr/bin/python
statement = raw_input("Enter string: ")
shift = raw_input("Enter shift amounts: ").split()
for index, char in enumerate(statement):
if index < len(shift):
print chr(ord(char) + int(shift[index])),
else:
print char,
Written for Python 2, and doesn't do error checking on user input, but should work.
Edit: And a Python3 version, since it looks like that's what you're using:
#!/usr/bin/python
statement = input("Enter string: ")
shift = input("Enter shift amounts: ").split()
for index, char in enumerate(statement):
if index < len(shift):
print(chr(ord(char) + int(shift[index])), end='')
else:
print(char, end='')
print()
In both versions, if the number of integers provided by the user for shift is less than the size of the string, a shift value of 0 is assumed.
If you wanted to, you could do a test to see if the number of shift integers provided by the user is less than the length of the string, and error out, or ask the user for input again.
Edit 2: As requested in comments, this code makes the shift restart instead of assuming it is 0:
#!/usr/bin/python
statement = input("Enter string: ")
shift = input("Enter shift amounts: ").split()
for index, char in enumerate(statement):
index = index % len(shift)
print(chr(ord(char) + int(shift[index])), end='')
print()
Edit 3: As requested in comments, this code makes sure user input is only a-z or A-Z, and is in a function.
#!/usr/bin/python
import sys
from string import ascii_letters as alphabet
def enc_string(statement, shift):
# Make sure user entered valid shift values
try:
shift = [ int(val) for val in shift.split() ]
except ValueError:
print("Error: All shift values must be integers")
sys.exit(1)
# Make sure all chars in statement are valid
for char in statement:
if char not in alphabet:
print("Error: all characters in string must be a-z or A-Z")
sys.exit(1)
# Start putting together result
res = "Result: "
# Convert statement
for index, char in enumerate(statement):
shift_to_use = shift[index % len(shift)]
index_of_char = alphabet.index(char)
index_of_new_char = (index_of_char + shift_to_use) % len(alphabet)
res += alphabet[index_of_new_char]
return res
statement = input("Enter string: ")
shift = input("Enter shift amounts: ")
print(enc_string(statement, shift))