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
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

the best b

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值