ChenFi 发表于 2020-8-6 21:51:52

大佬,求帮助!!!

password = list(input("please input your password: "))
times = 3
i = 0
length = len(password)
realpassword = list("chen")
while password == "*" and i <= length:
    password = list(input("密码中不能含有“*”号,你还有",times,"次机会,请输入密码:"))
while times > 0 and password != realpassword:
    times -= 1
    password = list(input("密码错误,你还有",times,"次机会:"))
if password == realpassword:
    print("密码正确,进入程序。。。")
else:
    print("密码错误,机器锁死")


运行程序后是这样
please input your password: 123456
Traceback (most recent call last):
File "D:/python/009-01.py", line 10, in <module>
    password = list(input("密码错误,你还有",times,"次机会:"))
TypeError: input expected at most 1 argument, got 3
>>>

sunrise085 发表于 2020-8-6 22:00:17

input只能有一个参数,就是提示作用的字符串,你却给出了三个参数。。
正常的写法应该是
input("密码错误,你还有%d次机会:"%times)
这样写,input函数只有一个参数,是一个字符串,只是字符串中有格式化字符。你的那种写法,会被认为是三个参数

Twilight6 发表于 2020-8-6 23:43:44


input 里面用格式化,你不能直接 , 逗号隔开,不然 Python 会认为你传入 3 个参数给 input 函数了,而 input 函数只能接受一个参数导致报错

正确代码,使用 字符串格式化即可,这里用 % 格式化:

password = list(input("please input your password: "))
times = 3
i = 0
length = len(password)
realpassword = list("chen")
while password == "*" and i <= length:
    password = list(input("密码中不能含有“*”号,你还有%s次机会,请输入密码:"%times))
while times > 0 and password != realpassword:
    times -= 1
    password = list(input("密码错误,你还有%s次机会:"%times))
if password == realpassword:
    print("密码正确,进入程序。。。")
else:
    print("密码错误,机器锁死")

陈尚涵 发表于 2020-8-7 10:19:58

Twilight6 发表于 2020-8-6 23:43
input 里面用格式化,你不能直接 , 逗号隔开,不然 Python 会认为你传入 3 个参数给 input 函数了,而 in ...

或者用format~

password = list(input("please input your password: "))
times = 3
i = 0
length = len(password)
realpassword = list("chen")
while password == "*" and i <= length:
    password = list(input("密码中不能含有“*”号,你还有%s次机会,请输入密码:"%times))
while times > 0 and password != realpassword:
    times -= 1
    password = list(input("密码错误,你还有{0}次机会:".format(times)))
if password == realpassword:
    print("密码正确,进入程序。。。")
else:
    print("密码错误,机器锁死")
页: [1]
查看完整版本: 大佬,求帮助!!!