For my programming class, we have to create a function that takes in a string argument and returns the rot13 equivalent of the string. When I try running my function, it says that count can't equal str[i] because string indices have to be integers. I'm honestly lost and what else I could do to make the function work. Any help would be lovely
def str_rot_13(str):
new_list = []
for i in str:
if ord(i) <= 77:
count = str[i]
k = chr(ord(count) + 13)
new_list.append(k)
if ord(i) > 77 and ord(i) <= 90:
count = str[i]
k = ord(count) - 78
new_list.append(chr(65 + k))
return new_list
for i in str:
if ord(i) <= 77:
count = str[i]
k = chr(ord(count) + 13)
In Python, for i in str
will loop through every character in the string str
, with i
set to that character (which you already know, since you're doing ord(i)
). (Don't use str
as a name, by the way: str
is the Python name for the string type.) count = str[i]
is treating the character in i
as an index. You don't need to (or should) do that. It doesn't make much sense.