python学习笔记------输入和while循环

1 用户输入

1.1 语法————input()

message = input("Tell me something, and I will repeat it back to you: ")
print(message)

1.2 获取数值

message = int(message);

1.3 求余(%)

4%3 ==> 1;
4%2 ==> 0;

注:输入之前不可以使用enter键,否则会认为输入为null;
python2.7使用的输入为raw_input();

2 while循环

2.1 格式

while [condition]:
	doSomeThing

2.2 循环控制

break-----退出整个循环
continue–结束本次循环,开始下一次
注:循环需指明判断条件,避免无限循环

2.3 操作列表

while list:
	doSomeThing

3 练习代码

#边界线,区分不同功能
divide = '###################################################';
#用户输入
message = input("Tell me something, and I will repeat it back to you: ")
print(message)
print(divide)
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print("\nHello, " + name + "!")
print(divide)
#获取数值
height = input("How tall are you, in inches? ")
height = int(height)
if height >= 36:
    print("\nYou're tall enough to ride!")
else:
    print("\nYou'll be able to ride when you're a little older.")
print(divide)
#while循环
current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1
print(divide)
##########while循环操作列表#########################
# 首先,创建一个待验证用户列表
# 和一个用于存储已验证用户的空列表
unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []
# 验证每个用户,直到没有未验证用户为止
# 将每个经过验证的列表都移到已验证用户列表中
while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    print("Verifying user: " + current_user.title())
    confirmed_users.append(current_user)
# 显示所有已验证的用户
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())
print(divide)
# 删除包含特定值的所有列表元素
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
    pets.remove('cat')
print(pets)
print(divide)

输出(* *为输入字体)

Tell me something, and I will repeat it back to you: **fmx**
fmx
###################################################
If you tell us who you are, we can personalize the messages you see.
What is your first name? **fxd**

Hello, fxd!
###################################################
How tall are you, in inches? **50**

You're tall enough to ride!
###################################################
1
2
3
4
5
###################################################
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confirmed:
Candace
Brian
Alice
###################################################
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
###################################################
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值