python-作业-2

小结

1.判断某个东西是否在某个东西里面,in or not in,返回结果是布尔值(false or true)

  name = "电磁辐射"
  v = "里" in name
  print(v)

2. 运算符

  + - * / ** % //

  %号求的是余数,例9%2=1,而//求的是商,如9//2=4  

3. break 与 continue 的区别  

i = 0
while i < 10:
    i = i + 1
    print(i)
    continue # or break
    print('test')

print('end')

  continue继续循环while里的条件,不执行下面的内容

  break 上面和下面的内容均不执行,直接跳出循环

4. while 与 If  都可以作为条件判断与else配用,但while可以循环if却不具备此功能。

i = 0
while i < 10:
    print(i)
    i = i + 1
else:
    print('else')
print('end')

--------------------------------------------------------------------------------------

作业:用户登陆(三次机会重试)

i = 0
while i < 3:
    user_input = input("请输入用户名:")
    user_pwd = input("请输入密码:")
    if user_input == "user" and user_pwd == "123456":
        print("欢迎登陆!")
        print(".............")
        break
    else:
        print("用户名或者密码错误")
    i = i + 1

猜你喜欢

转载自www.cnblogs.com/marypy/p/9045540.html