I am trying to do a function that shifts each letter in each word to the right by value and these words will be from a list that I will open it using "open"function I wrote the code, and I am facing some difficulties here here is my code
def test():
value=eval(input("Value here!"))
with open ("word-text.txt","r") as f:
for ord in (f):
print (ord)
for chr in ord:
print (chr)
#nice=(chr[len(ord)+value])
''.join([chr(ord(i)+2) for i in s])
print (i)
this is the output I get
Value here!2
apples
a
p
p
l
e
s
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
test()
File "/Users/user/Desktop/Outlook(1)/lab6.py", line 59, in test
''.join([chr(ord(i)+2) for i in s])
NameError: global name 's' is not defined
the list that I have the following words apples oranges watermelon kiwi zucchini carrot okra jalapeno pepper cucumber banana and when I use this code
ef test():
value=eval(input("Value here!"))
with open ("word-text.txt","r") as f:
for ord in (f):
print (ord)
for chr in ord:
print (chr)
nice=(chr[len(ord)+value])
print (nice)
I get this output
Value here!2
apples
a
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
test()
File "/Users/user/Desktop/Outlook(1)/lab6.py", line 58, in test
nice=(chr[len(ord)+value])
IndexError: string index out of range
>>>
What am I doing wrong here? and which code is best to use, to get it working
Thanks
While your approach is generally on a somewhat right track, there are some problems with it as others have noted. Since it seems that you're having trouble understanding what to do and how to go about solving the problem, I've tidied your code (made a few edits here and there) and heavily documented my edit of your code so you can understand what the approach should be:
The only problem with this approach is that it is not circular (i.e. - it doesn't loop back around: e.g.
z
shifted by3
does not becomec
), as the values will exceed the upper bound of their respective character set (as revealed in the OUTPUT section below). And, if the value entered is very large (i.e. greater than the bounds of the character set in which you're working, you'll get gibberish.However, I don't know if that's a requirement of your problem (which may not be as you're basically doing a primitive encryption of passwords as suggested by your previous questions), but I thought that I'd mention in nonetheless.
OUTPUT: