我的python学习笔记.用户输入.函数input()的工作原理
来源:互联网 发布:剑三冷艳花姐捏脸数据 编辑:程序博客网 时间:2024/06/10 10:25
函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,python将其存储在变量中,以方便使用。
#helloword.py
message=input("Tell me something,and I will repeat it back to you: ")
print(message)
输出为:
D:\www>python helloword.py
Tell me something,and I will repeat it back to you: haha
haha
实例之编写清晰的程序:
#helloword.py
prompt="If you tell us who you are,we can personalize the massages you see."
prompt+="\nwhat is your first name? "
message=input(prompt)
print("\nHello, "+message+"!")
输出为:
D:\www>python helloword.py
If you tell us who you are,we can personalize the massages you see.
what is your first name? jin
Hello, jin!
2.使用int()来获取数值输入
使用函数input()时,python将用户输入解读为字符串,可使用函数int(),它让python将输入视为数值。
>>> age=input("How old are you?")
How old are you?21
>>> age>=18
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>=' not supported between instances of 'str' and 'int'
>>> age=int(age)
>>> age>=18
True
将数值输入用于计算和比较前,务必将其转换为数值表示。
实例:
#helloword.py
height=input("How tall are you?")
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.")
输出为:
D:\www>python helloword.py
How tall are you?162
You're tall enough to ride!
3、求模运算符
#helloword.py
number=input("Enter a number,I'll tell you if it's even or odd:")
number=int(number)
if number%2==0:
print("\nThe number "+str(number)+" is even.")
else:
print("\nThe number "+str(number)+" is odd.")
输出为:
D:\www>python helloword.py
Enter a number,I'll tell you if it's even or odd:3
The number 3 is odd.
- 我的python学习笔记.用户输入.函数input()的工作原理
- Python学习笔记(3)用户输入&函数
- 我的python学习笔记.函数
- C++学习笔记5---虚函数的工作原理
- C++学习笔记27,虚函数的工作原理
- Python中partial函数的工作原理
- Python输入函数:raw_input() 与 input() 的区别
- python input输入函数
- 我的Python学习笔记(一)
- 我的python学习笔记(二)
- 我的python学习笔记(三)
- 我的Python学习笔记
- 我的python学习笔记
- 我的python学习笔记
- Struts1学习笔记(Struts的工作原理)
- Python学习笔记(6)-用户输入和while循环
- Python学习笔记(10)- 用户输入和while循环
- python学习笔记之(三)7.1用户输入
- mybatis学习之路----动态sql之trim标签源码详解,附带where标签解析
- Docker中使用CentOS7镜像
- bean的最后一次
- pubsub-js 实现RN组件间通信
- 使用swagger2生成RESTful客户端
- 我的python学习笔记.用户输入.函数input()的工作原理
- 简单协程
- 第一次作业
- 多线程--上下文切换
- 泛型 泛型的约束
- 关于jquery,mysql的一些小知识
- 杂记(查漏补缺)
- opencv像素基本操作及图像遍历at
- LeetCode #547 Friend Circles