5-2 更多地条件测试:
代码如下:
str1="AAAA"
str2="BBBB"
str3="AAAA"
print("str1==str2? "+str(str1==str2))
print("str1==str3? "+str(str1==str3))
num1=10
num2=60
num3=10
print(num1==num3 and str1==str2)
print(num1==num3 or str1==str2)
nums=[10,20,30,40]
print ( num1 in nums)
print(num2 not in nums)
运行结果:
5-4 外星人颜色#2:
代码如下:
saucers=['green','blue']
for s in saucers:
print("The colour of saucer man that you killed is "+s+". ",end=" ")
if s=='green':
print("You got 5 points!")
else:
print("You got 10 points!")
结果如下:
5-5 外星人颜色#3:
代码如下:
saucers=['green','yellow','red']
for s in saucers:
print("The colour of saucer man that you killed is "+s+". ",end=" ")
if s=='green':
print("You got 5 points!")
elif s=='yellow':
print("You got 10 points!")
elif s=='red':
print("You got 15 points!")
结果如下:
5-8 以特殊方式跟管理员打招呼:
代码如下:
saucers=['Lily','Jojo','Bob','admin','Mike']
for s in saucers:
if s=='admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello "+s+", thank you for logging in again.")
结果如下:
5-9 处理没有用户的情形:
代码如下:
saucers=['Lily','Jojo','Bob','admin','Mike']
saucers.clear()
if len(saucers)==0:
print("We need to find some users!")
else:
for s in saucers:
if s=='admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello "+s+", thank you for logging in again.")
结果如下:
5-10 检查用户名:
代码如下:
def check(a,users):
for b in users:
if(a.upper()==b.upper()):
return False
return True
current_users=['Lily','Jojo','Bob','John','Mike']
new_users=['JOHN','Judy','Lisa','Bob','James']
for s in new_users:
if check(s,current_users):
print("This name isn't used.")
else:
print("This name has been used. You need to input another one.")
结果如下: