Home > Back-end >  converting list to array or int in python
converting list to array or int in python

Time:06-26

import numpy as np

Original_word = str(input("What is the phrase you want to encrypt:"))

Y=int(input("what number do you want the letter to move forward:"))

n = [ord(x) for x in Original_word]

real_list = np.array(n)

Z=real_list Y

print(chr(ord('@') Z))

this is my python code and it shows TypeError: only integer scalar arrays can be converted to a scalar index. I have no idea how to solve this. Can anyone pls help me and pls don't show me this page Python TypeError : only integer scalar arrays can be converted to a scalar index as i already read it and have no idea how to implement it

CodePudding user response:

There's no need for numpy here, just use list comprehensions.

Original_word = str(input("What is the phrase you want to encrypt:"))

Y=int(input("what number do you want the letter to move forward:"))

n = [ord(x) for x in Original_word]
Z = [chr(x   Y) for x in n]
print(''.join(Z))

CodePudding user response:

It's giving the error because you're passing a list to chr()

This will work

import numpy as np

Original_word = input("What is the phrase you want to encrypt: ")
Y = int(input("what number do you want the letter to move forward: "))

n = [ord(x) for x in Original_word]
real_list = np.array(n)
Z = real_list   Y

temp = [chr(x   ord('@')) for x in Z]
temp_str = ''.join(temp)
print(temp_str)

I'll implement the code like this

str = input("What is the phrase you want to encrypt: ")
step = int(input("what number do you want the letter to move forward: "))

char_list = [ord(x) for x in str]
encrypted_string = ''.join(chr(x   step   ord('@')) for x in char_list)
print(encrypted_string)
  • Related