Here is my code:
file = open("codecipherinput.txt", "r")
while True:
line1 = file.readline()
line2 = file.readline()
#print(line1 + line2)
if not line2: break # EOF
currentline = line1 + line2
for i in range(3):
print(currentline)
s, text = currentline.strip().split(" ")
def encrypt(text, s):
result = ""
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) + s - 65) % 26 + 65)
else:
result += chr((ord(char) + s - 97) % 26 + 97)
return result
#text = input("Enter Word: ")
#s = int(input("Shift: "))
print("Cipher: " + encrypt(text, s))
These are the contents of my file:
7
rmpc is the best!
3
i’m loving it
23
there’s nothing more fun than comp-sci programming
I am trying to have the program take two lines at a time from the file and split those lines into two variables but am unsure how. Can someone help?
Use zip
, and it happens automatically:
for line1, line2 in zip(file, file):
...
zip
pulls from the first iterable, then from the second (except they're the same here, so it's just two lines), and then we unpack the resulting tuple
to named variables. zip
stops when either iterable is exhausted, matching the if not line2: break
code implicitly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.