简体   繁体   中英

converting list to array or int in python

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

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))

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM