这是一个家庭作业,但我已经把工作放在我的解决方案,不知道我是从哪里得到一个错误。以下是我应该做的事:
下面是我到目前为止掌握的代码:
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然而,这也不能解决我的问题。
任何帮助或指导都是非常感谢的!
发布于 2019-03-15 18:22:21
我建议在第二个循环中使用一个临时变量,这样就可以将其拆分,而不是使用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但是即使在这里,您的bstring也会被替换为每个ch in message。也许把它带到外面,这样你就可以继续添加到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这样,您的bstring就不仅仅是基于消息的最终特征。
https://stackoverflow.com/questions/55188351
复制相似问题