【问题标题】:how to do random caps (python)如何做随机大写(python)
【发布时间】:2022-11-10 21:18:17
【问题描述】:

嘿,所以我有一个项目,我们必须制作一个随机密码生成器,并且我需要在某个地方设置一个大写字母。所以我喜欢停止者密码,我需要它在某处有随机大写。有人可以帮忙吗

编辑:好的,所以基本上你需要这个人输入他们需要密码的网站,这就是为什么我喜欢让我们做停止密码,这样他们就可以输入网站,然后我们从那里更改 wesbite 名称 n 从我们可以拥有的密码ciper 中的随机字母大写 yk

到目前为止,这是我的代码(在随机大写部分之后,我计划询问他们的收藏编号,然后用符号添加它,然后打印他们的密码)

def caesar_encryption(plaintext,key):
  encryption_str = ''
  for i in plaintext:
    if i.isupper():
      temp = 65 + ((ord(i) - 65 + key) % 26) 
      encryption_str = encryption_str + chr(temp)                              
    elif i.islower():
      temp = 97 + ((ord(i) - 97 + key) % 26)
      encryption_str = encryption_str + chr(temp)
    else:
      encryption_str = encryption_str + i  

  print("The ciphertext is:",encryption_str)
 
plaintext = input("Enter the Website Name:")
key = int(input("Enter the key:"))
caesar_encryption(plaintext,key) 

【问题讨论】:

  • 你为什么使用凯撒密码?只需检查每个字母,随机将其更改为大写字母。
  • 欢迎来到堆栈溢出。我不明白这个问题。请阅读How to Ask - “有人可以帮忙吗”是not answerable。您认为解决问题需要哪些步骤?哪一部分你不知道怎么做?例如,你知道怎么做吗任何事物Python中的“随机”?究竟什么是“随机上限”?你的意思是一个特定的字母应该,根据随机决定,是大写还是小写,是吗?换句话说,你想做一个随机的选择在这两种可能性之间?
  • 好的,所以基本上你需要这个人输入他们需要密码的网站,这就是为什么我喜欢让我们做停止密码,这样他们就可以输入网站然后我们从那里更改 wesbite 名称 n 从密码中我们可以只使用随机字母在 ciper 中大写 yk @j1-lee

标签: python


【解决方案1】:

不需要凯撒密码。只需遍历每个字符,将其随机更改为大写字母。像这样:

import random

def random_caps(string):
    newstring = ""
    for ch in string:
        if ch.isalpha() and random.randint(0, 1):
            newstring += ch.upper()
        else:
            newstring += ch
    return newstring

您还可以使用这样的列表推导:

import random

def random_caps(string):
    return ''.join([ch.upper() if random.randint(0, 1) else ch for ch in string])

【讨论】:

    【解决方案2】:

    我必须这样做,这就是我所做的

    import random
        
    letters = ["A", "a", "B", "b", "C", "c"] #and so on
    numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] #and so on
    special_characters = ["/", "!", "@"] #and so on
        
    def password():
        n1 = random.choice(letters) #or any others
        n2 = random.choice(numbers) #or any others
        n3 = random.choice(letters) #or any others
        n4 = random.choice(special_characters) #or any others
        n5 = random.choice(numbers) #or any others
        n6 = random.choice(letters) #or any others
        n7 = random.choice(special_characters) #or any others
        n8 = random.choice(letters) #or any others
        password = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8
        print(password)
    password()
    

    【讨论】:

    • 此策略无法保证密码将包含任何大写字母
    • 是的,但 6/10 次有上限
    • 4/10 不会有任何上限。所以它不能满足要求
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2020-03-06
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2020-05-01
    • 2014-05-04
    相关资源
    最近更新 更多