栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

0基础学python(20)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

0基础学python(20)

函数input

input()让你的文本停止运行并且输出括号中的数据,让再赋给一个变量,之后让python输出。

message=input("123456789...")
print(message)
123456789...

我们在使用input函数的时候应该在输入的时候输入清晰的提示表明需要什么数据,例如:

name=input("plese enter your name:")
print(f"nhello,{name}!")

当我们输入名字Tom时将会输出。

please enter your name:Tom
hello,Tom!

有时候我们需要用户输出的提示可能不止一行,所以我们可以将提示符给一个变量,再将变量传递给函数input。
例如:

prompt = "if you tell us who you are ,we can personalize the messages you see."
promot+="nwhat is your first name?"
name=input(prompt)
print(f"nhello,{name}!")

最后将输出的是。

if you tell us who you are ,we can personalize the messages you see.
what is your first name?tom
hello,tom!

我们在使用input()输出数字的时候输出的是字符串这时候需要int,例如:

age=input("how old are you?")

当我们输出age=21的时候将会输出字符串‘21’
我们应该将变量age放在int()中进行输出
例如:

age=input("how old are you?")
age=int(age)

这时候输出的age将会是21。
在实际运用中我们函数int(),我们要判断一个人的年龄是否能能玩游戏。
例如:

age=input("how old are you ?")
age=int(age)
if age>=18:
	print("nyou age is enough")
else:
	print("nyou age is not enough")
		

我们使用age=int(age)将我们输入的数字进行转换。

求模运算符

求模运算符%是个很有用的工具他将两个数相除并返回余数:

4%3
1
5%3
2
6%3
0
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/320032.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号