#How to get rid of Whitespace in my Python Program

73 messages · Page 1 of 1 (latest)

valid dune
#

Files provided below

rocky gobletBOT
#

@valid dune

File Attachments Not Allowed

For safety reasons we do not allow files with certain file extensions.

Code Formatting

You can share your code using triple backticks like this:
```
YOUR CODE
```

Large Portions of Code

For longer scripts use Hastebin or GitHub Gists and share the link here

Ignored these files due to them having disallowed file extensions
  • PA_4.pdf
valid dune
#

I have two text files message.txt and code.txt

#

message.txt has "Hello! How are you?" written in it

valid dune
#
# 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.")
solemn fossil
heady holly
#

The question is a bit vague, what exactly do you want to remove?

solemn fossil
#

looks like his pic shows whitespace, he posted code in 2 parts

heady holly
#

Yeah but replacing whitespace is pretty easy lmao

valid dune
#

@solemn fossil @heady holly Yes but from the output newline

heady holly
#

encoded_message.replace('\n', '')

valid dune
#

Where exactly is the line?

#

@heady holly

#

Do Put that in?

heady holly
#

wherever you print it lol

#

Just replace newlines with an empty string before printing

valid dune
#

I get this weird highlgiht ting

#

@heady holly

heady holly
#

Because you're searching for it?

#

lol

valid dune
#

I thought I closed that haha

#

I am fairly new to this and not sure where to put the method

#

@heady holly

heady holly
#

Should be ok

valid dune
#

Really>

#

?

#

It should light up

heady holly
#

What

#

Ah line 71

#

The closing ) is too early

valid dune
#

I was experimenting but still no

heady holly
#

What is .add supposed to do there lol

valid dune
#

what the

#

Interesting that was not there

#

Now it works

#

Sorry I just woke up

#

But I don't want end= ' '

heady holly
#

wat

valid dune
#

I am just trying to find a method to get rid of this

#

for some reason it does not happen in the first one

heady holly
#

Probably forgot to add it in the other places where you're printing it...

valid dune
#

But when I do replace it gives me this

#

Essentially end= ' '

heady holly
#

What do you want the output to look like? Ciphertext: "stuff" in one line? Gotta use end or formatstrings for that

valid dune
#

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

heady holly
#

What do you want the output to look like

#

lol

valid dune
#

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

heady holly
#

Again

#

What do you want it to look like

valid dune
#

Ciphertext:
KHOOR!
KRZ DUH BRX?
Plaintext:
HELLO!
HOW ARE YOU?

heady holly
#

So then just print them out, does the text contain the quotes?

#

Looks exactly like the old output to me

valid dune
#

But there will be different inputs

heady holly
#

Or I guess you want double newlines replaced

#

So that would be .replace('\n\n', '\n')?

valid dune
#

Right right

#

Oh it worked

#

Thanks and could you check your dms?