I am working on a project is to convert text into numbers. (For example, "hello world" would be converted to "8 5 12 12 15 27 23 15 18 12 4").
In line 10 of my code, the for loop causes the following error message:
Traceback (most recent call last):
File "C:\Users\gabri\PycharmProjects\padding\main.py", line 15, in <module>
converter(plaintext)
File "C:\Users\gabri\PycharmProjects\padding\main.py", line 10, in converter
for n in plaintext:
TypeError: 'types.GenericAlias' object is not iterable
Process finished with exit code 1
My code is as follows:
alphabet = ("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')
def getplaintext():
global plaintext
plaintext = list[input("Enter plaintext:.....")]
print(plaintext)
def converter(plaintext):
for n in plaintext:
print(n)
getplaintext()
converter(plaintext)
Does anybody know what is causing this error?
You need to use list with (), not [].
plaintext = list(input("Enter plaintext:.....")) # With ()
In newer versions of Python, container types such as list can be hinted directly to specify the type of the elements the container holds. By using square braces here, you create a generic type hint of a list (a "generic alias"). A generic alias for a list is not iterable, as it is not actually a list. This is what causes your error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With