0

I am creating a code that will put messages into numbers based off of their position in my alp string and adding the value of the given key number. For example, if I want to code "HI" with key code of 2, it would be 9 10. Because H's position is 7, and I's position is 8, and we added 2 to each number position. Every time I run this, I get Error "Int not iterable":

def main():

    message=input("Enter your message to code: ")
    key=int(input("What is the key value?"))
    alp="ABCDEFGHIJKLMNOPQRSTUVWZYZabcdefghijklmnopqrstuvwxyz0123456789 .,?!"
    for letters in message:
        inString=int(alp.index(letters))
        print(inString)

    for numStr in inString:
        code=numStr+key
        print(code)

however, I tried to change it to this:

def main():

    message=input("Enter your message to code: ")
    key=int(input("What is the key value?"))
    alp="ABCDEFGHIJKLMNOPQRSTUVWZYZabcdefghijklmnopqrstuvwxyz0123456789 .,?!"
    for letters in message:
        inString=int(alp.index(letters))
        print(inString)

    for numStr in str(inString):
        code=(int(numStr)+key)
        print(code)



main()

this time, I got

Enter your message to code: HI
What is the key value?2



7
8
10
>>> 

What am I doing wrong?

8
  • Are you running Python2 or 3? Commented May 6, 2016 at 17:50
  • @busfault that's entirely valid syntax in Python2, and does exactly what you'd expect. Commented May 6, 2016 at 17:53
  • 2
    @busfault: It doesn't, because that syntax works on Python 2 and people do it. More telling is the lack of a NameError from input when the user types HI. Commented May 6, 2016 at 17:54
  • It prints 7 and 8 in the first loop because those are the positions of H and I within alp (remember, Python indexing begins at zero, not one). It then prints 10 in the second loop because inString is left with a value of 8 from the first loop, to which you add the value of key (which is 2). Commented May 6, 2016 at 17:57
  • @WayneWerner Doh. My apologies, I have rarely if ever seen print(...) in Python 2.x code. Commented May 6, 2016 at 17:59

1 Answer 1

1

You should probably use a list to store the numbers into and then you can iterate over that list.

inString = []
for letters in message:
    inString.append(int(alp.index(letters)))
    #print(inString)

for numStr in inString:
    code=(int(numStr)+key)
    print(code)

The 7 and 8 outputs you got were the right indexes. However, your inString was being overwritten on every loop. So in the end inString was just equal to 8 and the last for loop ran only once with the value 8.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried to add a loop within... How do I need to adjust this so that, it takes the position of each letter, and adds <key> to it? I did this and it still says int not iterable?
Because your inString is an integer and you can't iterate over integers. The code that I posted does exactly what you asked for in the question, so I'm not really sure why you would want to add more loops. You should probably also read about the logic behind for loops in general: tutorialspoint.com/python/python_for_loop.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.