#How to get rid of Whitespace in my Python Program
73 messages · Page 1 of 1 (latest)
@valid dune
For safety reasons we do not allow files with certain file extensions.
You can share your code using triple backticks like this:
```
YOUR CODE
```
For longer scripts use Hastebin or GitHub Gists and share the link here
- PA_4.pdf
I have two text files message.txt and code.txt
message.txt has "Hello! How are you?" written in it
# assignment: programming assignment 4
# author: [Your Name]
# date: [Date]
# file: cipher.py is a program that implements a Caesar cipher for encoding and decoding text
# input: User input from console, file input/output
# output: Console output, file output
# read text from a file and return text as a list of strings
def readfile(filename):
try:
with open(filename, 'r') as file:
return file.read().strip().replace('!','!\n')
except FileNotFoundError:
print("The selected file cannot be open for reading!")
return None
# write a list of strings (message) to a file
def writefile(filename, message):
try:
with open(filename, 'w') as file:
file.write(message)
except:
print("The selected file cannot be open for writing!")
# encode plaintext letter by letter using a Caesar cipher
def encode(plaintext, shift=3):
alphabet = tuple(chr(65 + i) for i in range(26))
ciphertext = ''
for char in plaintext:
if char.isalpha():
i = alphabet.index(char.upper())
ciphertext += alphabet[(i + shift) % 26]
else:
ciphertext += char
return ciphertext
# decode ciphertext letter by letter using a Caesar cipher
def decode(ciphertext, shift=-3):
return encode(ciphertext, -shift)
# An optional helper function
def to_string(text):
s = ''
for line in text:
s += line
return s
if __name__ == '__main__':
while True:
print("Would you like to encode or decode the message?")
print("Type E to encode, D to decode, or Q to quit:")
choice = input().upper()
if choice == 'E':
filename = input("Please enter a file for reading: ")
message = readfile(filename)
if message is not None:
encoded_message = encode(message, 3) # Fixed shift value to 3
print("Plaintext:")
print(message.upper()) # Print the message in all uppercase
print("Ciphertext:")
print(encoded_message) # No need for join or replacing spaces
writefile(input("Please enter a file for writing: "), encoded_message)
elif choice == 'D':
filename = input("Please enter a file for reading: ")
encoded_message = readfile(filename)
if encoded_message is not None:
decoded_message = decode(encoded_message, 3) # Fixed shift value to 3
print("Ciphertext:")
print(encoded_message) # No need for join or replacing spaces
print("Plaintext:")
print(decoded_message) # No need for join or replacing spaces
writefile(input("Please enter a file for writing: "), decoded_message)
elif choice == 'Q':
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
can you post the whole code on pastebin etc.
https://www.pythonmorsels.com/paste/
The question is a bit vague, what exactly do you want to remove?
looks like his pic shows whitespace, he posted code in 2 parts
Yeah but replacing whitespace is pretty easy lmao
encoded_message.replace('\n', '')
wherever you print it lol
Just replace newlines with an empty string before printing
I thought I closed that haha
I am fairly new to this and not sure where to put the method
@heady holly
Should be ok
What is .add supposed to do there lol
what the
Interesting that was not there
Now it works
Sorry I just woke up
But I don't want end= ' '
wat
I am just trying to find a method to get rid of this
for some reason it does not happen in the first one
Probably forgot to add it in the other places where you're printing it...
What do you want the output to look like? Ciphertext: "stuff" in one line? Gotta use end or formatstrings for that
Would you like to encode or decode the message?
Type E to encode, D to decode, or Q to quit:
e
Please enter a file for reading:
message.txt
Please enter a file for writing:
code.txt
Plaintext:
HELLO!
HOW ARE YOU?
Ciphertext:
KRZ DUH BRX?
Would you like to encode or decode the message?
Type E to encode, D to decode, or Q to quit:
d
Please enter a file for reading:
code.txt
Please enter a file for writing:
plain.txt
Ciphertext:
KHOOR!
KRZ DUH BRX?
Plaintext:
HELLO!
HOW ARE YOU?
Would you like to encode or decode the message?
Type E to encode, D to decode, or Q to quit:
q
Goodbye!```
I tried end and it makes everything on one line.
I don't know how that white space came to be
Ciphertext:
KHOOR!
KRZ DUH BRX?
Plaintext:
HELLO!
HOW ARE YOU?
elif choice == 'D':
filename = input("Please enter a file for reading: ")
encoded_message = readfile(filename)
if encoded_message is not None:
decoded_message = decode(encoded_message, 3) # Fixed shift value to 3
print("Ciphertext:")
print(encoded_message) # No need for join or replacing spaces
print("Plaintext:")
print(decoded_message) # No need for join or replacing spaces
writefile(input("Please enter a file for writing: "), decoded_message)
It should be maybe in this block of code?
Becasue E works fine
Ciphertext:
KHOOR!
KRZ DUH BRX?
Plaintext:
HELLO!
HOW ARE YOU?
So then just print them out, does the text contain the quotes?
Looks exactly like the old output to me
But there will be different inputs