[英]What should be my arguments in my function and what should i return
我在Python中有一个简单的问题。 编写一个程序,要求用户输入密码。
如果用户输入正确的密码,程序将告诉他们他们已登录到系统。
否则,程序应要求他们重新输入密码。 用户只能尝试输入密码五次,此后程序应告诉他们已将其踢出系统。
我已经解决了这个问题,但是我不知道我的函数中是否需要参数。 另一个问题是我应该在程序中返回什么。 我把return 0放了,但我不希望0出现在调试器中。
def code():
global password
password='123456'
global total_guesses
total_guesses=5
while total_guesses<=5 and total_guesses>0:
resposta=input('Digite password\n')
if password==resposta:
print('You have entered the system')
break
else:
print('Wrong password you haver',total_guesses,'total_guesses')
total_guesses-=1
return 0
print(code())
您不需要任何参数...您也不需要返回任何内容,该函数将在完成时退出。
如果希望用户控制password
和password
,可以将password
和total_guesses
作为函数的输入。
同样,是否要返回内容也取决于您,如果密码正确,则可以返回True
,否则返回False
,如果您想根据这些值做出进一步的决定,例如,如果用户登录,则打开一个UI,或者如果用户无法登录,关闭程序等。或者无法返回任何内容,这取决于用户登录/登录失败后的下一步
def code(password, total_guesses):
#Flag to return if user logged in or not
logged_in = False
while total_guesses<=5 and total_guesses>0:
resposta=input('Digite password\n')
if password==resposta:
print('You have entered the system')
logged_in = True
break
else:
print('Wrong password you haver',total_guesses,'total_guesses')
total_guesses-=1
#Return the flag
return logged_in
password='123456'
total_guesses=5
#Use password and total_guesses as inputs
print(code(password, total_guesses))
输出看起来像
Digite password
123456
You have entered the system
True
Digite password
1
Wrong password you haver 5 total_guesses
Digite password
2
Wrong password you haver 4 total_guesses
Digite password
3
Wrong password you haver 3 total_guesses
Digite password
4
Wrong password you haver 2 total_guesses
Digite password
5
Wrong password you haver 1 total_guesses
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.