python输入end退出循环_Python 学习笔记 - 用户输入和while循环

1.用户输入函数 input() 的工作原理

函数 input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用。

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

Tell me something, and I will repeat it back to you: hello Python!

>>> print(message)

hello Python!

当你使用 input() 时,应该指定清晰而易于明白的提示,准确地指出你希望用户提供什么样的信息。

2.使用 int() 来获取数值输入

使用函数 input() 时,Python将用户输入解读为字符串,所以一旦你想用用户输入的数据来跟其他类型比较时,就必须做类型转换。

>>> age = input("How old are you ?")

How old are you ?30

>>> age

'30'

如上所示我们得到了一个 30 的字符串。

>>> age >= 30

Traceback (most recent call last):

File "", line 1, in

age >= 30

TypeError: '>=' not supported between instances of 'str'

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值