[英]I want all characters in a string to increase, except for certain ones
I am working on a small script to encrypt a string that the user provides.我正在编写一个小脚本来加密用户提供的字符串。 The user can also choose how much each character in the string should increment by (on a scale from 1 to 25).用户还可以选择字符串中每个字符的增量(范围从 1 到 25)。
But, I want certain characters to not increment at all (commas, periods, question marks, and spaces) and I have these in a list.但是,我希望某些字符根本不增加(逗号、句点、问号和空格),并且我将它们放在一个列表中。 How exactly is the possible?究竟有多大可能?
Below is the Python Script.下面是 Python 脚本。
user_str = str(input("Enter String: "))
user_inc = int(input("How much do you want to increase by: "))
if user_inc > 25:
print("You cannot increase by a number larger than 25.")
elif user_inc <= 0:
print("You cannot increase by a number lower than 1.")
else:
final_str = ""
for letter in user_str:
new_str = ord(letter)
enc_str = new_str + user_inc
if enc_str > 122:
enc_str -= 26
# Here is where I need help.
if letter in [',', '.', ' ', '?']:
letter + enc_str
final_str += chr(enc_str)
print(final_str)
I've tried a few different things like trying to add or subtract nothing from the letter, trying to add letter to the final_str, and so far nothing has worked.我尝试了一些不同的方法,例如尝试从字母中添加或减去任何内容,尝试将字母添加到 final_str,但到目前为止没有任何效果。 How exactly can this be done?这到底是怎么做到的?
You use an if statement to determine whether to add the original value, or the new value to the ciphertext.您使用 if 语句来确定是将原始值还是新值添加到密文中。
[...]
else:
final_str = ""
for letter in user_str:
new_str = ord(letter)
enc_str = new_str + user_inc
if enc_str > 122:
enc_str -= 26
# Here is one idea
if letter in [',', '.', ' ', '?']:
final_str += letter
else:
final_str += chr(enc_str)
print(final_str)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.