[Python] Delete the last two characters and print them out

Asked 2 years ago, Updated 2 years ago, 33 views

After receiving data in the word variable in the following code, enter "Q" and 0 in order to exit and output the data. If I run this code, how do I code to subtract "Q" and 0 and output it?

python python3

2022-09-22 15:42

1 Answers

The logic you want is unclear, but look at the code below.

while True:
    Word = raw_input() + '\n' # Take one line.
    Locate end = word.find("Q0") # "Q0".
    If end > -1: # If you found "Q0"
        file.write(word[:end]) # Write only before "Q0".
        break # Stop typing.
    else: # If you didn't find it
        infile.write(word) # Write all.

And when you upload the code, it's easier to answer by copying and pasting it, not by pictures.


2022-09-22 15:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.