20200329_WEEK1

一、依然是Hello World!

学会一门语言的第一句话就是

print('Hello World!')

二、输入用户名和密码

  1. 通过input来达到交互的效果;
  2. 可以使用getpass模块来以密文的形式显示;
  3. 检验用户名和密码输入的正确与否;
  4. { }和format同时使用,给变量随机赋值。
_username = 'Jack'
_password = 'Jack123'

username = input('name:')
password = input('password:')
if username == _username and password == _password:
    print('welcome ',(username))
elif username == _username and password != _password:
    print('wrong password')
else:
    print('{name} does not exist'.format(name = username))

三、猜数字

  1. 导入random模块,使用随机数;
  2. 用户输入数字;
  3. 提供3次输入机会,且用完之后可向用户再次确认是否重试;
  4. if else; while;
import random
secret = random.randint(0,10)
count = 0
while count < 3:
    guess = int(input('GUESS THE NUMBER:'))
    count += 1
    if guess == secret:
        print('you are brilliant')
        break
    elif guess < secret:
        print('it is bigger')
    else:
        print('it is smaller')
    if count == 3:
        continue_confirm = input('try again or not?')
        if continue_confirm != 'no':
            count = 0
else:
    print('you have try so many times, game over')

四、continue和break

  1. continue 跳出本次循环,进入下一次循环
  2. break 终止循环
for i in range(3):
    print('---i---')
    for j in range(5):
        print('---j---',j)
        if j >3:
            continue

执行结果:

---i---
---j--- 0
---j--- 1
---j--- 2
---j--- 3
---j--- 4
---i---
---j--- 0
---j--- 1
---j--- 2
---j--- 3
---j--- 4
---i---
---j--- 0
---j--- 1
---j--- 2
---j--- 3
---j--- 4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值