Python学习入门之用户输入

Python学习入门之用户输入

用户输入函数 input()

函数input让程序暂停运行,等待用户输入一些文本,获取用户输入后,将其存储到在一个变量中
使用input函数,python将用户输入解读为字符串
input函数接受一个参数:即要向用户显示的提示或说明
简单示例

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

使用int函数将数字的字符串表示转换为数值表示

age = input("Please input your age:")
age = int(age)
if age >= 18 :
    print("you are old enough to vote.")
else:
    print("sorry,you are too young to vote.")

求模运算符(%)

它将两个数相除并返回余数

num = int(input("Enter a number,and I'll tell you if it's even or odd:"))
if num % 2 ==0:
    print("\n the number " + str(num) + " is even.")
else:
    print("\n the number " + str(num) + " is odd.")

在python 2.7中获取输入

如果使用的是python 2.7,应使用raw_input()来提示用户输入。这个函数与python 3中的input()一样,也将输入解读为字符串。
python 2.7中也包含input(),但它将用户输入解读为为python 代码,并将尝试运行它们。因此,最好的结果是出现错误,指出python不明白输入的代码;最糟糕的结果是,将运行原本无意运行的代码。因此,如果使用的是 python 2.7 ,请使用raw_input()而不是input()来获取输入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值