-3

Hi I'm having a tough time trying to incorporate more than 1 shift in my program. The goal is to assign a shift amount to each character like:

statement = input("Enter string: ")
shift = input("Enter shift amounts: ")

For example, if

statement = "Hello"

shift = 5 6 7 

How do I assign that 5 number to H, 6 to E, 7 to l, then again 5 to l, and 7 to o

I think that I need to make it into a list so this is what I have thus far:

for char in statement:
    char[0] = shift[0]
Chris
  • 35
  • 4
  • What should be done about the different cases (upper, lower) in the input string? What should be done with non-alphabetic characters? How are you getting the shift amounts into one or more Python variables? – Rory Daulton Apr 09 '17 at 18:18
  • hmm I never thought about upper and lower. chr and ord should do the trick but I just don't understand how I can enter more than one shift value to correspond with each character – Chris Apr 09 '17 at 18:23
  • You did not answer my questions, so your problem is not yet clear. It would be easiest to just ignore the case, returning all lower case (for example), and just leave non-numeric characters unchanged. And I want to see that you understand an important part of the assignment given to you--getting the shift amounts out of the string the user provides. – Rory Daulton Apr 09 '17 at 18:26
  • It looks like I need to implement an if statement to only take alphabetic letters like alphabet = 'abcdefghijklmnopqrstuvwxyz' if char in alphabet: char[0] + shift[0] – Chris Apr 09 '17 at 18:31

1 Answers1

0

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))
Tal
  • 322
  • 1
  • 7
  • 15
  • ohhh I get it I completely forgot about .split(). Definitely makes it easier than feeding them all through manually – Chris Apr 09 '17 at 18:34
  • How do I make it so that the shift amounts reset in each line after "5, 6, 7" so that the shifts keep iterating through each character ("5,6,7,5,6,7,5,6,7, etc.). Do you think that the continue function would work? Or would you suggest making a loop – Chris Apr 09 '17 at 19:40
  • Thanks and do you think the code would look better if I added in a function? And if I only wanted the shifts to account for the alphabet can I do a: alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' if char in statement = alphabet – Chris Apr 09 '17 at 20:45
  • That look better? – Tal Apr 09 '17 at 22:06
  • Mark the question as solved then so others know there's a solution. – Tal Apr 09 '17 at 22:44
  • Ah sorry man I'm still having a tough time implementing this I will when I have it done – Chris Apr 09 '17 at 23:19
  • I’m trying to make it so any character not part of the alphabet isn’t shifted but it does take up a shift amount so that if a space character is shifted by say 3 characters the next character after the space character would be shifted by 4. I'm sure I'll get it at some point but I'll mark it as solved when done – Chris Apr 09 '17 at 23:22
  • Ya I'm not sure how I'd do this have been trying for 4 hours. Do you think I should implement an if statement? – Chris Apr 10 '17 at 01:24
  • No disrespect, but if you're not sure if you "should implement an if statement", you really need to go back to the basics. There are dozens of free Python tutorials online. Most of the code above is fairly beginner-level, except maybe the list comprehension, but even that is fairly easy to wrap your head around. – Tal Apr 10 '17 at 01:35
  • Yep I'm definitely a beginner and my teacher isn't that great. I've done code academy and while I do know how to use an if statement I'm just having a tough time really applying that concept to this problem – Chris Apr 10 '17 at 01:39
  • Knowing how to use an if statement won't get you very far in any programming language - it's the most basic statement there is. To understand the above code you would need to read up on: basic exception handling, python data types, functions, for loops, membership testing, importing other modules, list comprehension, and indexing, at the very least. None of these topics are particularly difficult - if you have any programming experience in any language, this is probably a weekend of reading. If not, a couple of hours a day for a week will probably get you going. – Tal Apr 10 '17 at 01:47
  • you're right I should probably concentrate harder this is basics stuff in the scope of it. Thanks – Chris Apr 10 '17 at 01:49