首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未使用循环结束的加密和解密项目

问未使用循环结束的加密和解密项目
EN

Stack Overflow用户
提问于 2021-03-13 03:46:20
回答 1查看 54关注 0票数 1

我一直在做一个有趣的小项目,致力于加密一些消息,然后可能解密它们。唯一的问题是,当我调用我的类的实例时,它会到达,但之后它再也不会回来并继续运行。有没有解决这个问题的建议,谢谢!

代码语言:javascript
复制
import datetime

beta_value = {"A":5, "B":9, "C":11, "D":12, "E":13, "F":14, "G":8, "H":6, "I":7, "J":15, "K":16, "L":4, "M":17, "N":18, "O":19, "P":20, "Q":10, "R":21, "S":22, "T":23, "U":24, "V":3, "W":25, "X":26, "Y":27, "Z":28, "": 0, ".": 1, "!": 2}

#Find the day
d = datetime.datetime.now()
day = d.strftime("%d")

#The beta value is the value for each letter in the alphabet that is the number of nodes it takes to construct the capital counter part of that letter, and +n if needed

#Create the encryption and decryption class
class Beta():
    def __init__(self, sen):
        self.liste = []
        self.listd = []

        #Leaf through the letter for encryption or decryption
        for let in sen:
                self.liste.append(self.encrypt(let))
                self.listd.append(self.decrypt(let))

        # resulte = 
        # resultd = 

    #Create encryption
    def encrypt(self, let):
        allowed = True
        beta_uf = int((int(beta_value[let.capitalize()]) + int(day)) % 28)

        #Check if the beta value exists in the beta_value list
        while allowed:
            for key, value in beta_value.items():
                if int(beta_uf) == value:
                    beta_f = key
                    allowed = False
        if allowed:
            beta_uf += 1

        return beta_f

    #Create decryption
    def decrypt(self, let):
        pass

#Input words
words = input("What is your text? ")

#Create instance of the class
B = Beta(words)

#Encrypt or decrypt?
e_or_d = input("Encrypt or Decrypt? ")

if e_or_d == "encrypt" or "Encrypt":
    print(B.liste)
else:
    print(B.listd)

另外,请不要批评我这有多糟糕。

跟进:嗨,下面的评论帮助摆脱了我的主要问题(我也从上面更新了我的代码到新的东西),但现在我想尝试包括空格,但似乎对那些给予的人有问题:

代码语言:javascript
复制
Traceback (most recent call last):
  File "main.py", line 49, in <module>
    B = Beta(words)
  File "main.py", line 19, in __init__
    self.liste.append(self.encrypt(let))
  File "main.py", line 28, in encrypt
    beta_uf = int((int(beta_value[let.capitalize()]) + int(day)) % 28)
KeyError: ' '

哈哈,nvm,我修好了.:P

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-13 04:25:58

在你的代码中有几件事情可能是他们自己的问题,然而这是关于为什么你的代码“卡住了”并且没有返回任何值。

这是因为您的while循环没有达到退出条件,并且陷入了无限循环。

您的while循环基本上是这样做的:

代码语言:javascript
复制
while True:
    for i in range(10):
        break

在for循环内部发出的break只会从for循环中“退出”,而不会触及while循环的状态。

有多种方法可以解决这个问题。解决这个问题的一种方法是进一步的流控制和使用for-loops (经常被遗忘)else-语句。

只有在执行完整个for循环的情况下,才会执行for循环后面的else语句。

让我们用这段代码来看看它的实际效果:

代码语言:javascript
复制
>>> for i in range(3):
...     print(i)
... else:
...     print("The for-loop was executed to its end.")
... 
0
1
2
The for-loop was executed to its end.

如果我们在continue语句中使用这种行为,那么我们可以使while循环“未命中”它自己的break语句。

下面是一个(不那么简单)的例子:

代码语言:javascript
复制
import random
while True:
    for i in range(3):
        rint = random.randint(0,10)
        print(f'range {i=} {rint=}')
        if i == rint:
            print("Breaking the for-loop.")
            break
    else: # Only runs when the whole for-loop was executed.
        print("Continuing to the next for-loop!")
        continue
    print("Breaking the while-loop.")
    break

一次运行的输出:

代码语言:javascript
复制
range i=0 rint=6
range i=1 rint=0
range i=2 rint=0
Continuing to the next for-loop!
range i=0 rint=0
Breaking the for-loop.
Breaking the while-loop.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66606278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档