2013-03-19 73 views
1

我想做一個簡單的小程序,告訴用戶密碼是錯誤的。
但是,當我想讓程序回來他們輸入的密碼,並說這是錯誤的,它不會讓我。有人幫我..簡單的密碼程序

username = raw_input("Please enter your username: ") 
password = raw_input("Please enter your password: ") 
fail1 = raw_input("Your password is very insecure, make a new one: ") 
print "Your password [password] is too weak!" 
+0

你說的 '它不會讓你'?你有沒有更多的代碼顯示給我們? – toxotes 2013-03-19 19:47:16

+0

你是什麼意思,它不會讓你?它錯誤嗎? – Hoopdady 2013-03-19 19:47:41

回答

3

Python不會神奇格式字符串,你需要做的是明確

print "Your password {} is too weak!".format(password) 

更多信息,如何.format()Format string syntax方法起作用。

另外,使用任何的:

print "Your password", password, "is too weak!" 
print "Your password %s is too weak!" % password 
import string 
template = string.Template("Your password ${password} is too weak!") 
print template.substitute(password=password) 
+3

爲了滿足各地語法愛好者的需求,請確保您將它改爲「**太弱**!」 (有兩個),然後再實施。 (我知道這是怎麼回事,@MartijnPieters) – PaulStock 2013-03-19 20:16:05

+0

你走了,至少在我的答案中,語法平衡恢復。 – 2013-03-19 21:16:23

0

如果你想顯示裏面另外一個字符串,你需要使用%。例如:

print "Your password [%s] is too weak" % password 
0

嘗試:

print "Your password %s is to weak!" % password 
0
print "Your password", password, "is to weak!"