Posts: 5
Threads: 2
Joined: Jul 2018
Jul-09-2018, 08:15 PM
(This post was last modified: Jul-09-2018, 08:21 PM by zer0.)
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'
Posts: 8,160
Threads: 160
Joined: Sep 2016
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
Posts: 5
Threads: 2
Joined: Jul 2018
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
Posts: 566
Threads: 10
Joined: Apr 2017
Jul-09-2018, 10:10 PM
(This post was last modified: Jul-09-2018, 10:10 PM by volcano63.)
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
- With
str.isdigit
message = '' # Fake value to start a loop
while not message.isdigit():
message = input("Enter a number you want to be decrypted: "))
- 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.
Posts: 5
Threads: 2
Joined: Jul 2018
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
Posts: 566
Threads: 10
Joined: Apr 2017
Jul-09-2018, 10:29 PM
(This post was last modified: Jul-09-2018, 10:29 PM by volcano63.)
(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  IDLE
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.
Posts: 5
Threads: 2
Joined: Jul 2018
Umm, yes I use IDLE, is that bad? And how do I use UTF-8?
Posts: 566
Threads: 10
Joined: Apr 2017
(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  )
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.
|