0

i am writing some code, and i have a problem. i have the functions i wrote working fine, but the main loop (which should work indefinetely) only correctly works once. here is the code:

while 1==1:
    choice=int(raw_input("press 1 for encrypt, 2 for decrypt"))
    if choice==1:
        string=raw_input("please enter plaintext here\n")
        print('cipher-text follows and was copied to clipboard "'+encrypt_string(string))+'"'
    elif choice==2:
        string=raw_input("please enter cipher-text here\n")
        print('plaintext follows : "'+decrypt_string(string))+'"'
    else:
        print("please enter a valid option")

the problem follows that the whole loop works once, but then it proceeds to skip the raw_Input command and throw up a value error. i cant see why it would do this. any ideas?

edit, the error:

Traceback (most recent call last):
  File "C:\Users\Raffi\Documents\python\encryptor.py", line 37, in <module>
    choice=int(raw_input("press 1 for encrypt, 2 for decrypt"))
ValueError: invalid literal for int() with base 10: ''
  • 1
    Please [edit] your question with the **full text** of any errors you are getting. – MattDMo Dec 12 '14 at 21:58
  • 3
    *What line* throws a value error? What does `decrypt_string()` do? – Martijn Pieters Dec 12 '14 at 21:58
  • Are you using Python 2 or 3? Or are you using `from __future__ import print_function`? (Note that in 3, `raw_input` was replaced with `input` and `print` is a function, not a statement.) – Makoto Dec 12 '14 at 21:59
  • Given that traceback, it looks like you pressed enter twice in a row, and the second keystroke gave Python an empty line of input. – Kevin Dec 12 '14 at 22:00
  • @MAttDMo decrypt_string takes in a string, edits it, and then outputs a string. – Raphael Treccani-Chinelli Dec 12 '14 at 22:01
  • @Kevin, thats what i thought was happening but i havent been pressing enter which is leading me to believe either a \n is doing the same thing or choice already being set is affecting it but i dont know – Raphael Treccani-Chinelli Dec 12 '14 at 22:02
  • @Makoto the `print()` function was backported to Python 2.6, and at least in 2.7 you don't need the `from __future__ ...` statement. I do agree that the version of Python is important here, though... – MattDMo Dec 12 '14 at 22:05
  • @MattDMo: I hadn't realized that. Kind of got to get the old python 3.0.x days out of my head... – Makoto Dec 12 '14 at 22:06
  • I'm guessing that at some point you're pasting text into the terminal? – jme Dec 12 '14 at 22:08
  • @jme yes thats right i had a function to copy text to the clipboard and then i paste it back in. looks like when i enter a single character it works fine, but multiple characters mess it up. will be playing around for a while, thanks for all everyones help :) – Raphael Treccani-Chinelli Dec 12 '14 at 22:11
  • Yes, you're probably pasting two newline characters or similar. `raw_input` only reads up to the first newline, so on the second iteration of the `while` it reads a newline and that's it. – jme Dec 12 '14 at 22:21
  • @MattDMo no, you still need to use `from __future__...` to get the print function in Python 2.7. https://docs.python.org/2/library/functions.html#print – Stuart Dec 12 '14 at 23:21
  • @Stuart it depends on your system. On Windows, my 2.7.8 interpreter accepts `print(foo)` and `print foo` both (after defining `foo`, obviously...) – MattDMo Dec 12 '14 at 23:52
  • @MattDMo No, it doesn't depend on the system. The print statement ignores parentheses. Type `print(print)` to see that it is a statement rather than a function. – Stuart Dec 12 '14 at 23:59

1 Answers1

0

It seems (as per the comment by jme) that when you are pasting text into the terminal there are multiple newline characters that cause your programme to proceed to enter nothing for the next raw_input command. There are a couple of possible solutions from an earlier question:

(1) use sys.stdin.read():

print("please enter plaintext here\n")
text = sys.stdin.read()

However, you then have to press Ctrl Z / Ctrl D to indicate you have finished entering text.

(2) use Tk.clipboard_get or similar commands to read from the clipboard directly.

from Tkinter import Tk
root = Tk()
root.withdraw()
...
text = raw_input('Please enter plain text or just press enter to copy from the clipboard\n')
if not text:
    text = root.clipboard_get()

(3) You could also continue allowing input until a blank line, or other way of marking the end of the text, is entered.

print('Please enter plain text. Enter "stop" when you are finished')
text = ''
while True:
    inp = raw_input()
    if inp == 'stop':
        break
    text += inp

Of course, this assumes that there are no lines saying 'stop' in the text that you want to copy.

Community
  • 1
  • 1
Stuart
  • 9,597
  • 1
  • 21
  • 30