简体   繁体   English

我希望字符串中的所有字符都增加,除了某些字符

[英]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.

相关问题 删除除具有特定条件的行以外的所有行 - Dropping all rows except ones with a certain criteria 如何缩放除 pandas dataframe 中的某些列之外的所有列? - How to scale all columns except certain ones in pandas dataframe? Select 除了在 BeautifulSoup 中具有某些类的所有 div - Select all divs except ones with certain classes in BeautifulSoup 如何删除DataFrame中除某些列外的所有列? - How to delete all columns in DataFrame except certain ones? 匹配除特定单词之外的所有字符 - Match all characters except for certain words 删除所有数字,除了使用 python regex 组合成字符串的数字 - Remove all numbers except for the ones combined to string using python regex 我想使用正则表达式对方括号内的所有字符进行转义,但在python中该括号的开始和结尾除外 - I want to escape all characters within square brackets except start and end of that bracket in python using regex 如何删除所有与模式匹配的单词,但我要保留的某些单词除外(它们与模式匹配) - How to remove all words matching a pattern, except certain words which I want to preserve?(they match the pattern) 计算字符串中除元音和标点符号之外的所有字符 - counting all characters in a string except for the vowels and punctuations Python 用下划线替换字符串中除 3 个字符外的所有字符 - Python replace all characters in a string except for 3 characters with underscores
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM