小编Che*_*rry的帖子

如何限制python上的用户输入长度?

amt = float(input("Please enter the amount to make change for: $"))
Run Code Online (Sandbox Code Playgroud)

我希望用户以美元输入金额,因此允许5个字符(00.00)有没有办法限制它,所以它不允许他们输入超过5个字符?

我不想要这样的东西,它允许你输入超过5但会循环.

while True:
amt = input("Please enter the amount to make change for: $")
if len(amt) <= 5:
        print("$" + amt)
        break
Run Code Online (Sandbox Code Playgroud)

我希望完全限制输入超过5个字符

python

9
推荐指数
1
解决办法
516
查看次数

标签 统计

python ×1