简体   繁体   English

Python:如何获得一个程序来提示用户一定的固定时间来输入用户信息?

[英]Python: How do you get a programme to prompt the user a certian fixed amount of times for a user input?

In the programme the user must enter a password, if he gets it wrong the programme will prompt him 2 more times and if he does not get right those 2 times the programme will expire. 在该程序中,用户必须输入密码,如果输入的密码错误,该程序将再次提示他两次,如果两次输入的密码都不正确,则该程序将过期。 How do I go about doing this. 我该怎么做。 This is my programme so far... 到目前为止,这是我的程序...

password=input("Enter password:")
while password != 'cat': 
    print ("password is wrong, try it again")
    password= input ("Enter your password:")

print ("Password correct be happy")

The charitable programmer 慈善程序员

for trial in range(3):
    print ("Attempt no.",trial, end=" ")
    passw = input('.  Enter password > ')
    if passw == 'cat' : break
else:
    passw = 'cat'

Yes, also the humble for loop has an else clause: "Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement." 是的,谦虚的for循环还具有else子句: “循环语句可能具有else子句;当循环通过用尽列表(带有for)终止循环或当条件变为false(带有while)时执行该语句,但不是当循环由break语句终止时。” .

try this: 尝试这个:

import sys
max_tries = 3; current_tries = 0
password=input("Enter password:")
while password != 'cat': 
    if current_tries > max_tries:
        print "too many tries"
        sys.exit(0)
    print ("password is wrong, try it again")
    password= input ("Enter your password:")
    current_tries +=1


print ("Password correct be happy")
max_tries = 3
password = None
for i in range(max_tries):
    password = input("Enter Password:")
    if password == "cat": break
if password != "cat": 
    print ("ERROR TOO MANY TRIES!")
else:
    print ("You Win!")

is a simple way to do it ... 是一种简单的方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何让 Python 向用户询问输入“x”的次数? - How do you get Python to ask the user for an input 'x' amount of times? 我如何从列表中随机选择用户要求在 python 中使用输入的次数? - how do i randomly pick characters out of a list the amount of times the user asks for using input in python? 如何在 vscode 中的 python 中获得用户输入? - How do you get a user input in python in vscode? Python:如何提示/输入用户 - Python: How to prompt/input the user 如何使用 python 通过用户输入在范围内打印一定数量的结果? - How do you print certain amount of results in range by user input using python? 当用户为变量分配了用户选择输入的“ x”次次数时,如何创建变量? - How do I create variables as they are being assigned by the user a 'x' amount of times that the user chooses to input? 当用户输入不正确时如何从特定点重复代码 - How to repeat code from a certian point when user input is incorrect 在python提示后如何显示用户输入? - How to display user input after prompt in python? 如何根据用户对第一个问题的输入提示用户 x 次 - How do I promt user x amount of times based on their input on the first question 如何进行验证循环以确保用户在 a.split 输入中输入正确的次数? - How do I make a validation loop to make sure the user inputs the correct amount of times in a .split input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM