![](/img/trans.png)
[英]How can I convert an excel function that is shown below into a python pandas code?
[英]How can I use conditionals in python to solve the code shown below?
我的目的是创建一个包含“用户名”、“密码”和“年龄”的用户字典,并使用条件来测试字典中的 object 是否会被接受,就像网站上的登录页面一样。 但由于我犯了一个错误,我没有得到我想要的结果。 我需要做什么才能使条件产生我想要的结果? 由于字典是可变的,我能做些什么来确保用户名和密码是不可变的?
user1 = {
'username': 'Casper',
'age': 26,
'password': 'schism'
}
user2 = {
'username': 'Stone',
'age': 32,
'password': 'kalamari'
}
user_name = input('What is your username: ')
pass_word = input('What is your password: ')
if user_name and pass_word == user1['username' and 'password']:
print(' Your details are correct')
else:
print('Incorrect details')
我猜你在问三元运算符
句法:
[on_true] if [expression] else [on_false]
答:
print("correct") if pass_word ==user1['password'] and user_name == user1['username'] else print("wrong")
编辑1:
print("correct") if [pass_word,user_name] ==[user1['password'],user1['username']] else print("wrong")
你可以这样做,但尽量避免这样写,它会降低代码的可读性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.