函数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