Python Forum

Full Version: New to coding - Python error: TypeError: unsupported operand type(s) for -: 'str' and
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am new to coding so I was trying to test unicode and 'encrypting' stuff if you can even call it like that.I currently use Windows 10 and my Python version is 3.7, anyways I got that message, this is the code: (NOTE: I made an 'encryption' thing too but it worked and this wont)

while True:
    try:
        message = int(input("Enter a message you want to be decrypted: "))
        break
    except ValueError:
        print("Error, please enter an integer")
secret_string = ""
for char in message:
    secret_string += str(chr(char - str(742146)))
print("Decrypted:", secret_string)
q = input("")
Error:
Traceback (most recent call last): File "C:\Users\Aleksa\Desktop\decrypt.py", line 6, in <module> secret_string += str(chr(char - str(742146))) TypeError: unsupported operand type(s) for -: 'str' and 'str'
Please, post the entire traceback that you get. We need to see that whole thing. Do not just give us the last line.
Take a time to read What to include in a post

Also, fix the indentation of your code
I've been working on a simple 'project' basically what it does is that I insert an message and it translates it to unicode and it adds like 7839 to it so it would be 'secret'. Process of transforming it into unicode works but problem occurs when I wrote 'decrypt' thing, I am running windows 10 and python 3.70. Here is the code and the error:

while True:
    try:
        message = int(input("Enter a message you want to be decrypted: "))
        break
    except ValueError:
        print("Error, please enter an integer")
secret_string = ""
for char in message:
    secret_string += str(chr(char - str(742146)))
print("Decrypted:", secret_string)
q = input("")
Error:
Traceback (most recent call last): File "C:\Users\Aleksa\Desktop\decrypt.py", line 8, in <module> for char in message: TypeError: 'int' object is not iterable
int converts your message to an integer - and you cannot iterate over an integer - you need iterable for that, in any Python version. You can check that message may be represented as integer in 2 ways
  1. With str.isdigit
    message = '' # Fake value to start a loop
    while not message.isdigit():
        message = input("Enter a number you want to be decrypted: "))
  2. With try/except
    while True:
        message = input("Enter a number you want to be decrypted: ")
        try:
            int(message)
            break
        except ValueError:
            print("Error, please enter an integer")

I would pick the former - but the latter is tolerant to spaces surrounding numbers

PS
str(chr(char - str(742146)))
Would not work, did you mean
chr(ord(char) + 7839)
Now it's :
Error:
Traceback (most recent call last): File "C:\Users\Aleksa\Desktop\decrypt.py", line 11, in <module> print("Decrypted:", secret_string) UnicodeEncodeError: 'UCS-2' codec can't encode character '\U000b5339' in position 0: Non-BMP character not supported in Tk


Please help, anyone
(Jul-09-2018, 10:17 PM)zer0 Wrote: [ -> ]Now it's :
Error:
Traceback (most recent call last): File "C:\Users\Aleksa\Desktop\decrypt.py", line 11, in <module> print("Decrypted:", secret_string) UnicodeEncodeError: 'UCS-2' codec can't encode character '\U000b5339' in position 0: Non-BMP character not supported in Tk


Please help, anyone
Just add in your module before the code
# encoding: utf-8

PS I suppose you use Naughty IDLE Doh
Umm, yes I use IDLE, is that bad? And how do I use UTF-8?
(Jul-09-2018, 10:46 PM)zer0 Wrote: [ -> ]Umm, yes I use IDLE, is that bad?
Worth Python REPL (interactive shell) - as far as I know. Standard encoding is UTF-8 (at least, on Linux). Try your code either at repl.it or at online Jupyter notebook. Also, for a beginner, I would not recommend 3.7

(Jul-09-2018, 10:46 PM)zer0 Wrote: [ -> ]And how do I use UTF-8?
You set your encoding for a script by inserting into it the line I have given you above. It will set your encoding during the script execution. How to set encoding for IDLE - no idea (haven't touched it for years, never again Snooty )