【问题标题】:Encryption code in def function to be written in python用python编写的def函数中的加密代码
【发布时间】:2018-04-26 13:34:39
【问题描述】:

在以下代码中需要一些帮助,因为它进入无限循环并且不验证用户输入:get_offset 是函数。刚刚编辑需要一些帮助,以在定义的函数中完成加密部分

def get_offset(offset):
    while True:
        value = (offset)
        if value < 1:
            print("")
        if value > 94:
            print("")
    return offset

my_details = display_details()

get_choice = get_menu_choice()

print(my_details)
print(get_choice)

count = 10

while count > 0:

    count -=1

    encrypted = ""

    choice = int(input("What would you like to do [1,2,3,4,5]: "))

    while choice <1 and choice > 5:
        print("Invalid choice, please enter either 1, 2, 3, 4 or 5.")

    if choice == 1:
        string_input = input("Please enter string to encrypt: ")
        input_offset = get_offset(int(input("Please enter offset value (1 to 94): ")))

   **for letter in string_input:
        x = ord(letter)
        encrypted += chr(x + input_offset)
        if x < 32:
            x += 94
        if x > 126:
            x -= 94

    print(encrypted)**

【问题讨论】:

  • 在你的第一个 while 循环中,你永远不会改变“偏移量”,所以它每次都在做同样的事情 ==> 无限循环
  • 那么阿里需要编辑什么?我是初学者
  • @JaySadat 为了便于阅读,我编辑了您的帖子。您的代码需要缩进以支持降价,以便读者能够区分您的要求和您的代码是什么。
  • JaySadat:连续两次重复同一个问题是完全不可接受的:你之前在 Apr 23 上问过,说 *“请帮帮我。我需要一个快速回复。我真的很感激吗?”,我回答。你所做的只是抱怨答案。然后你昨天又问了。这种行为会让你在 SO 上被禁止。

标签: python validation input


【解决方案1】:

您必须在内部 while 循环中分配 choice 新值。这样,在一次又一次迭代之前会考虑新的输入。

这应该可以解决问题:

while choice < 1 and choice > 5:
    choice = int(input("Invalid choice, please enter either 1, 2, 3, 4 or 5."))

一个随机的建议:如果你想保持数据集严格等于 [1,2,3,4,5],你应该避免choice&lt;1choice&gt;5。这使您的程序容易受到无效输入的影响,例如3.5

相反,您应该这样做:

valid_inputs = [1, 2, 3, 4, 5]

choice = int(input("Pick a number from [1, 2, 3, 4, 5]:"))

while choice not in valid_inputs:
    choice = int(input("Invalid choice, please enter either 1, 2, 3, 4 or 5."))

<rest of stuff here>

我从您的 cmets 了解到的是,在 get_offset() 中,您希望用户输入一个介于 1 和 94 之间的值。如果用户输入的内容超出该范围,您就想一次又一次地提问。下面的代码应该做你想做的:

def get_offset():
   # first, ask for a value.
   offset = int(input("Enter a value between 1 and 94:"))
   while offset < 1 and offset > 94:
      # if the value is invalid, trap user inside this while loop.
      # user will be stuck here (while loop will return true) 
      # until a valid input is received.
      offset = int(input("Invalid input. Please enter a value between 1 and 94:"))
   # if we proceed down here, it means we are over while loop 
   # and it implies that user has given us valid input. return value.
   return offset

其次,在主while循环中,你可以像这样调用get_offset()

if choice == 1:
    string_input = input("Please enter string to encrypt: ")
    input_offset = get_offset()

【讨论】:

  • 我在询问选择 == 1 部分中的 get_offset
  • 哦,好吧,我弄错了。但是,一旦您输入了无效的输入,您仍然会被困在 while 循环中。
  • 你想在get_offset()做什么?
  • 我想将用户偏移输入限制在 1 到 94 之间。如您所见,我创建了一个函数并在偏移输入变量中使用它,但它仍然不起作用
  • 你能更清楚你想做什么吗?用户偏移输入是什么意思?您可以通过示例输入和输出来指定您的目标吗?
猜你喜欢
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 2015-10-08
  • 2012-12-11
  • 2013-12-02
  • 1970-01-01
  • 2015-01-27
  • 2015-07-30
相关资源
最近更新 更多