2016-11-12 80 views
0

我創建Python的一個程序,讓您創建一個用戶名和密碼,這是我到目前爲止有:Python的用戶名和密碼的創造者

#Code 
username = str(input("Please enter a username:")) 
print("Your username is",username,",proceed?") 
raw_input = input() 
if raw_input() == 'no': 
    re_u = input("Please re-enter username") 
else: 
    import getpass 
    mypass = getpass.getpass("Please enter your password:") 

而且我收到的麻煩是: 回溯(最近呼叫的最後一個): 第6行 if raw_input()=='no': TypeError:'str'對象不可調用。

請幫

+0

是你在Python 2.7或3.x版寫這個? –

回答

0
的raw_input

是一個字符串,但你想在它的結束與()來使用它作爲一個功能。用這個來代替:

if raw_input == 'no': 
1

在線路:

raw_input = input() 

如果您正在使用python 2.7,您覆蓋默認raw_input並使其成爲一個字符串。

如果您正在使用python 3,你正在創建一個字符串raw_stringinput()

所以此值,在這兩種情況下,當您嘗試調用raw_input像一個功能,您將收到錯誤。

TypeError: 'str' object is not callable.

if raw_input() == 'no': 

這裏,raw_input是一個字符串,而不是調用。你不能稱它爲一個函數。

現在,這樣的工作代碼應該有如下:(一個Python 2.7的解決方案)

import getpass 

username = raw_input("Please enter a username:") 
username_confirmation = raw_input("Your username is " + username + ",proceed?") 
if username_confirmation == "No": 
    username = raw_input("Please re-enter username") 
else: 
    password = getpass.getpass("Please enter your password:") 

# do something with username and password 
+0

如果這是Python 3,那麼'raw_input'不再是默認方法,因爲它改名爲'input'。 – Makoto

+0

@Makoto我覺得這是Python 2,因爲OP肯定是一個初學者,並且錯誤地使用'raw_input()'。如果這是Python 3,他不應該熟悉它。無論如何,我已經編輯了這兩種情況的答案。 –

+0

'print'成爲Python 3中的一個函數,'input'的行爲與Python 2中的raw_input相同(就像在Python 2中一樣,'input'也做了不同的事情)。我非常懷疑OP是使用Python 2,但感謝您糾正它。 – Makoto