Python编程从入门到实践(第七章用户输入和while循环学习总结)

#第七章用户输入和while循环#
1、input()函数让程序暂停运行,等待用户输入一些文本
2、int()函数来获取数值
3、求模运算符%返回两个数相除的余数
4、Python2.7中获取输入使用raw_input函数而不是input()函数
5、while循环
6、用户选择退出时
7、使用标志
8、break退出循环,不再运行循环中余下的代码
9、循环中使用continue
10、使用while循环在列表之间移动元素
11、删除包含特定值的所有列表元素
12、使用用户来输入来填充字典

################
1、input()函数让程序暂停运行,等待用户输入一些文本

name = input("Please enter your name: ")
print("Hello " + name + "!")

#输出结果:

Please enter your name: Tom
Hello Tom!


prompt = "If you tell us who you are,we can personalize the messages you see."
prompt += "\n What's your first name: "   #运算符+=在存储在prompt中的字符串末尾附加一个字符串
name = input(prompt)
print("\n Hello! " + name)

#输出结果:

If you tell us who you are,we can personalize the messages you see.
 What's your first name: Lily

 Hello! Lily

#Sublime Text 不能运行提示用户输入的程序。可以使用Sublime Text来编写提示用户输入的程序,但必须从终端运行它们

################
2、int()函数来获取数值
#将数字的字符串标识转换为数值表示

height = input("How tall are you: ")
height = int(height)  #转换成数值表示
if height >= 120:
    print ("\nYou are tall enought to ride!")
else:
    print ("\nYou'll be able to ride when you are a little older.")

#输出结果:

How tall are you: 110

You'll be able to ride when you are a little older.

################
3、求模运算符%返回两个数相除的余数

number = input("Enter a number,and I will tell
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值