Python 3.x Python-将字符转换为位字符串

Python 3.x Python-将字符转换为位字符串,python-3.x,ascii,Python 3.x,Ascii,这是一个家庭作业,但我已经把工作放在了我的解决方案中,不确定哪里会出错。以下是我应该做的: 向每个字符的数字ASCII值添加1 将其转换为位字符串 将字符串的位向左移动一个位置 以下是我目前掌握的代码: message = input("Enter a message: ") ordstore = 0 for ch in message: ordstore = ord(ch) + 1 bstring = "" while ordstore > 0:

这是一个家庭作业,但我已经把工作放在了我的解决方案中,不确定哪里会出错。以下是我应该做的:

  • 向每个字符的数字ASCII值添加1
  • 将其转换为位字符串
  • 将字符串的位向左移动一个位置
  • 以下是我目前掌握的代码:

    message = input("Enter a message: ")
    ordstore = 0
    
    for ch in message:
        ordstore = ord(ch) + 1
        bstring = ""
        while ordstore > 0:
            remainder = ordstore % 2
            ordstore = ordstore // 2
            bstring = str(remainder) + bstring
    
    #print(bstring)
    
    if(len(bstring) > 0):
        move1 = bstring[0]
        new_string = bstring[1:]
        new_string += move1
    
    print(new_string)
    
    我的问题是,我正在覆盖存储在第一个for循环中的值。我最初的想法是使用:

    ordstore = ord(ch) += 1
    
    然而,这也不能解决我的问题


    非常感谢任何帮助或指导

    我建议在第二个循环中使用一个临时变量,这样就可以将其分解,而不是使用ordstore变量

    for ch in message:
        ordstore = ord(ch) + 1
        bstring = ""
        tempordstore = ordstore 
        while tempordstore > 0:
            remainder = tempordstore % 2
            tempordstore = tempordstore // 2
            bstring = str(remainder) + bstring
    
    但即使在这里,您的
    b字符串也会替换消息中的每个
    ch
    。也许把它带到外面,这样你就可以继续添加到
    bstring

    bstring = ""
    for ch in message:
        ordstore = ord(ch) + 1
        tempordstore = ordstore 
        while tempordstore > 0:
            remainder = tempordstore % 2
            tempordstore = tempordstore // 2
            bstring = str(remainder) + bstring
    

    这样,您的
    b字符串就不仅仅基于消息的最后一个字符。

    我建议在第二个循环中使用一个临时变量,这样您就可以将其分解,而不是使用ordstore变量

    for ch in message:
        ordstore = ord(ch) + 1
        bstring = ""
        tempordstore = ordstore 
        while tempordstore > 0:
            remainder = tempordstore % 2
            tempordstore = tempordstore // 2
            bstring = str(remainder) + bstring
    
    但即使在这里,您的
    b字符串也会替换消息中的每个
    ch
    。也许把它带到外面,这样你就可以继续添加到
    bstring

    bstring = ""
    for ch in message:
        ordstore = ord(ch) + 1
        tempordstore = ordstore 
        while tempordstore > 0:
            remainder = tempordstore % 2
            tempordstore = tempordstore // 2
            bstring = str(remainder) + bstring
    
    这样,您的
    b字符串就不仅仅基于消息的最后一个字符了