+1 vote

Secret Message

Description

You write all your passwords in a diary so that you don't forget them. But clearly this is too risky, so you came up with a simple plan, you will simply write it by shifting all the alphabets by a certain step. For eg: if you decide your step to be 3, then 'a' will become 'd', and 'k' will become 'n' and so for all alphabets. The last alphabets will simply circle back to 'a'. In this case, 'y' will become 'b' and so on. Now you just have to remember the step size, can then you can check the password anytime you want. You decided to write code to do this, now that you have learned coding in python. Your code will take in the step size and what is written in the diary and give out the real password.

----------------------------------------------------------------------
Input:
A list of two elements.
The first element will be a string consisting of only alphabets which is taken from the diary and the second element will be the step size.

Output:

----------------------------------------------------------------------
Sample input:
['ytKveh' 4]

Sample output:

Explanation:
u became y,
p became t,
G became K,
r became v,
a became e,
d became h,
and thus what was written in the diary was ytKveh

----------------------------------------------------------------------
Sample input:
['banana', 7]

Sample output:
utgtgt

Execution time limit

Default. Dec 2, 2019 in Python 11,872 views
```import ast
mylist=ast.literal_eval(input())
stringMessage=mylist
shift_value=mylist
#start writing your code from here
def shift_string(message, n):
list1=[]
for i in message:
ch = i
base = ord('a' if ch.islower() else 'A')
x = chr((ord(ch) - base - n) % 26 + base)
list1.append(x)
return("".join(list1))
print(shift_string(stringMessage,shift_value))```

## 2 answers to this question.

Hi @Vaibhav! In such a case what you can do is,

ask the user for the password(which will be in the string format)

take each letter of the string and convert it into a character and use the ord() which will return its Unicode.

To this Unicode, you can add the count value and convert it back to a string.

Finally, print it'

You can use the following code:

```password = input("Enter the password")
count = input("Enter the count")
final_pass = ""
y = chr(ord(x) + count)
final_pass = final_pass + y
print(final_pass)``` answered Dec 5, 2019 by
• 52,350 points
it does't works for i/p banana and 7
This will give issue when the last alphabet is Z and the requirement is to have only alphabets in the output.

Refer below code for your problem statement

```def swap_letters(word, step_count):
final_word = ""
for x in list(word):
#         for lowercase letters crossing the Ascii values
if(ord(x)-step_count<97 and ord(x)>96):
w = 123-abs(97-(ord(x)-step_count))
y = chr(w)
final_word = final_word + y

#           for uppercase letters crossing the Ascii values
elif(ord(x)-step_count<65 and ord(x)<91):
w = 91-abs(65-(ord(x)-step_count))
y = chr(w)
final_word = final_word + y

#           for normal letters within the Std Ascii Values
else:
y = chr(ord(x)-step_count)
final_word = final_word + y
return final_word

swap_letters(input(),int(input()))``` answered May 17, 2020 by Kumar Sambhawam

edited May 18, 2020 by Gitika

## ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Hello @kartik, Since different users might have different ...READ MORE

## The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I couldn't figure out the problem. here my ...READ MORE

## Python join: why is it string.join(list) instead of list.join(string)?

950down voteaccepted It's because any iterable can be ...READ MORE

## Python join: why is it string.join(list) instead of list.join(string)?

This is because join is a "string" ...READ MORE

## Why is it string.join(list) instead of list.join(string)?

It's because any iterable can be joined ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE