没有合适的资源?快使用搜索试试~ 我知道了~
一、用户输入 函数 .input() : msg = input("Tell me something, and I will repeat it back to you: ") print(msg) 函数input()接受一个参数;在这个示例中,python运行第一行代码时,用户将看到提示: Tell me something, and I will repeat it back to you: 程序等待用户输入,并在用户按回车键后继续运行。 输入存储在变量msg中,接下来的print(msg)将输入输出。 当必要的提示不止一行时,我们可以: #先将部分提示存入一个变量,再使用+=运算符
资源详情
资源评论
资源推荐

Python学习记录学习记录5:基础知识:基础知识-用户输入和用户输入和while循环循环
一、用户输入一、用户输入
函数函数 .input() :
msg = input("Tell me something, and I will repeat it back to you: ")
print(msg)
函数input()接受一个参数;在这个示例中,python运行第一行代码时,用户将看到提示:
Tell me something, and I will repeat it back to you:
程序等待用户输入,并在用户按回车键后继续运行。
输入存储在变量msg中,接下来的print(msg)将输入输出。
当必要的提示不止一行不止一行时,我们可以:
#先将部分提示存入一个变量,再使用+=运算符接上剩余的提示
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "What is your first name? "
name = input(prompt)
print("Hello, " + name + "!")
output:
If you tell us who you are, we can personalize the messages you see.
What is your first name? Ming
Hello, Ming!
使用 int() 来获取数值输入数值输入:
input会将输入理解为字符串,我们需要int来转换。将数值用于计算和比较前,务必务必将其转化为数值表示数值表示,避免不必要的错
误。
height = input("How tall are you, in inches? ")
height = int(height) #转化为数值
if height >= 36:
print("You're tall enough to ride!")
else:
print("You'll be able to ride when you're a little older.")
求模运算符:% (同同c/c++)
*python 2.7
中应使用
: raw_input()
来获取输入。
二、二、while循环循环
1、简述:、简述:
prompt = "Tell me something, and I will repeat it back to you:"
prompt += "Enter 'quit' to end the program. "
active = True #随便定义的一个标志变量
while active: #while的用法与c/c++类似,区别是不用括号
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
*break/continue
在
python
中的用法与
c/c++
中相同。
2、用、用while循环来处理列表和字典:循环来处理列表和字典:
在列表间移动元素在列表间移动元素:
#首先,创建一个待验证用户列表
#和一个用于存储已验证用户的空列表
unconfirmed_users = ['alice', 'brian', 'candace'] confirmed_users = []
#验证每个用户,直到没有未验证用户为止
#将每个经过验证的用户都移动到已验证用户列表中
while unconfirmed_users:
current_user = unconfirmed_users.pop()



















weixin_38729336
- 粉丝: 7
- 资源: 925

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制

评论0