20200329_WEEK1
一、依然是Hello World!
学会一门语言的第一句话就是
print('Hello World!')
二、输入用户名和密码
- 通过input来达到交互的效果;
- 可以使用getpass模块来以密文的形式显示;
- 检验用户名和密码输入的正确与否;
- { }和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))
三、猜数字
- 导入random模块,使用随机数;
- 用户输入数字;
- 提供3次输入机会,且用完之后可向用户再次确认是否重试;
- 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
- continue 跳出本次循环,进入下一次循环
- 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