【问题标题】:How can I hide the user input being typed into raw_input如何隐藏输入 raw_input 的用户输入
【发布时间】:2018-08-27 07:10:45
【问题描述】:

我正在创建一个程序,该程序通过 raw_input 获取用户输入并将其放入代码中的文本文件中(例如 a = 2,c = 6)。我希望能够在用户按 Enter 之前隐藏用户在程序中输入的内容(然后显示编码文本)

代码如下:

#!/usr/bin/python
npt = raw_input (("26 Char Cyper Sentence: ").lower())
L = list(npt)
r = raw_input ('{name of file}: ')
txt='.txt'
me=r+txt
folder = open(me, 'a')
while True:
 a = L[0];b = L[1];c = L[2];d = L[3];e = L[4];f = L[5];g = L[6];h = L[7];
 i = L[8];j = L[9];k = L[10];l = L[11];m = L[12];n = L[13];o = L[14];p = L[15];
 q = L[16];r = L[17];s = L[18];t = L[19];u = L[20];v = L[21];w = L[22];x = L[23];y = L[24];z = L[25]
 txtt = raw_input (('{\n').lower())
 txt = txtt.lower ()
 new1 = txt.replace ('a', a)
 new2 = new1.replace ('b', b)
 new3 = new2.replace ('c', c)
 new4 = new3.replace ('d', d)
 new5 = new4.replace ('e', e)
 new6 = new5.replace ('f', f)
 new7 = new6.replace ('g', g)
 new8 = new7.replace ('h', h)
 new9 = new8.replace ('i', i)
 new10 = new9.replace ('j', j)
 new11 = new10.replace ('k', k)
 new12 = new11.replace ('l', l)
 new13 = new12.replace ('m', m)
 new14 = new13.replace ('n', n)
 new15 = new14.replace ('o', o)
 new16 = new15.replace ('p', p)
 new17 = new16.replace ('q', q)
 new18 = new17.replace ('r', r)
 new19 = new18.replace ('s', s)
 new20 = new19.replace ('t', t)
 new21 = new20.replace ('u', u)
 new22 = new21.replace ('v', v)
 new23 = new22.replace ('w', w)
 new24 = new23.replace ('x', x)
 new25 = new24.replace ('y', y)
 new26 = new25.replace ('z', z)
 print (new26.lower ())
 folder.write ((new26).lower ())

我在 Windows 10 上使用 Python 2.7,并且已经尝试过“getpass”模块和“curses”模块(我都无法工作:()。不幸的是,我无法向您展示我是如何使用这些模块的废弃了我制作的每个副本,但请随时向我展示如何(正确)在上面的代码中使用这些模块。我能做什么?而且,如果我只是改变输入的颜色,它会工作吗字体...以及如何?

【问题讨论】:

  • 请向我们展示您的代码以及您如何使用 getpass 库。
  • 好的,我现在添加。
  • 上面的代码工作正常,但没有隐藏输入。
  • 你试过了吗:import getpass; pwd = getpass.getpass(prompt='你的密语:');
  • 如果我错了,请纠正我,您似乎正在从事加密项目,但 Cyper 不是您拼写 cipher 的方式。

标签: python python-2.7 input


【解决方案1】:

请使用getpass python模块隐藏用户的输入。

一个最小的工作示例是:

import getpass
secret_word = getpass.getpass(prompt="Enter your secret word: ")
print(secret_word)

其中secret_word 是您要在终端中隐藏的单词。

更新

在您的情况下,该行将是:

txtt = getpass.getpass (('{\n').lower())

【讨论】:

  • txtt = getpass.getpass (('{\n').lower()) 是我使用该建议的更正行。谢谢:)
猜你喜欢
  • 2013-08-19
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 2012-06-16
  • 2011-12-06
  • 2022-11-15
  • 1970-01-01
  • 2017-06-27
相关资源
最近更新 更多