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?
inputwhen the user typesHI.7and8in the first loop because those are the positions ofHandIwithinalp(remember, Python indexing begins at zero, not one). It then prints10in the second loop becauseinStringis left with a value of 8 from the first loop, to which you add the value ofkey(which is 2).print(...)in Python 2.x code.