Python Forum
New to coding - Python error: TypeError: unsupported operand type(s) for -: 'str' and
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to coding - Python error: TypeError: unsupported operand type(s) for -: 'str' and
#1
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'
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
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)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
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
Reply
#6
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
Umm, yes I use IDLE, is that bad? And how do I use UTF-8?
Reply
#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 )
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 525 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  Coding error. xflyerwdavis 2 535 Oct-07-2023, 07:08 PM
Last Post: deanhystad
  Wrong type error rowan_bradley 6 1,234 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 1,211 Jul-22-2023, 11:33 PM
Last Post: jhancock
  Coding error. Can't open directory EddieG 6 1,138 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Coding Error EddieG 2 550 Jul-09-2023, 02:59 AM
Last Post: EddieG
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,279 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,957 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,336 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,181 May-07-2022, 08:40 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020