message = input("Enter a message you want to be revealed: ") secret_string = "" for char in message: secret_string += str(chr(char + 7429146)) print("Revealed", secret_string) q = input("")
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-182-49ece294a581> in <module> 2 secret_string = "" 3 for char in message: ----> 4 secret_string += str(chr(char + 7429146)) 5 print("Revealed", secret_string) 6 q = input("") TypeError: can only concatenate str (not "int") to str
while True: try: message = int(input("Enter a message you want to be decrypt: ")) break except ValueError: print("Error, it must be an integer") secret_string = "" for char in message: secret_string += chr(ord(char - str(742146))) print("Decrypted", secret_string) q = input("")
The Python "TypeError: can only concatenate str (not "int") to str" occurs when we try to concatenate a string and an integer. To solve the error, convert the int to a string, e.g. str(my_int) to concatenate the strings or convert the str to an int, e.g. int(my_str) to add the numbers.
The Python "TypeError: can only concatenate str (not "list") to str" occurs when we try to concatenate a string and a list. To solve the error, access the list at a specific index to concatenate two strings, or use the append() method to add an item to the list.
In Python, we cannot concatenate a string and an integer together. They have a different base and memory space as they are completely different data structures.
Python working a bit differently to JavaScript for example, the value you are concatenating needs to be same type, both int or str...
So for example the code below throw an error:
print( "Alireza" + 1980)
like this:
Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> print( "Alireza" + 1980) TypeError: can only concatenate str (not "int") to str
To solve the issue, just add str to your number or value like:
print( "Alireza" + str(1980))
And the result as:
Alireza1980
instead of using " + " operator
print( "Alireza" + 1980)
Use comma " , " operator
print( "Alireza" , 1980)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With