![](/img/trans.png)
[英]How to add numbers in a list, if some of the numbers are appended from user input?
[英]Add multiple numbers taken from user input and append it to a list
我只是在这里有一个小程序,我想用来从用户那里获取输入(就像输入密码一样)。 我有一个包含密码的列表,然后我将用户输入并将其放在一个空类中。 然后将该类与其中包含密码的类进行比较,如果匹配,则返回“good”。 但是我只能用一位数来做到这一点。 如何允许用户使用多个整数? 这是一种做这种事情的有效方式吗? 有更快更有效的方法吗? 谢谢。
class KeyCode(object):
def access(self):
room_code = [1]
print "Before you enter you must provide the room code: "
attempt = []
code = int(raw_input('>>'))
attempt.append(code)
if attempt == room_code:
print "Good"
else:
return 'death'
class Boxing_room(KeyCode):
def enter(self):
print "This is the boxing studio"
return 'Gymnast_room'
不一定需要列表。 您可以只比较字符串,或者如果您的代码只是数字,整数。
此外,一堂课在这里并不是真正有用(除非它只是为了了解它们)。 一个函数就足够了:
def access():
room_code = 12534
code = int(raw_input('Enter the code: '))
if code == room_code:
return 'good'
return 'death'
您可以使用字典来存储密钥代码:
code_dict = {'Boxing':'12345', 'Locker':'00000'}
并测试
if code_input == code_dict['Boxing']:
...
我同意Haidro的回答,但在我看来你可能想要允许多个密码?
如果是这种情况,您只需要进行“IN”检查。
例如。
def access():
room_code = [12345,54321]
code = int(raw_input('Enter the code: '))
if code in room_code:
return 'good'
return 'death'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.