本文共 821 字,大约阅读时间需要 2 分钟。
death_age=80
#input接收的所有数据都是字符串类型,即便你输入的是数字也会被当成字符串来处理
name=input("yourname:")
age=input("yourage:")
print(type(age))
print("yourname:",name)
print("yourcanstilllivefor",death_age-int(age),"years.....")
print("yourcanstilllivefor"+str(death_age-int(age))+"years.....")
>>>
your name:nihao
your age:30
your name: nihao
your can still live for 50 years.....
your can still live for 50 years.....
缩进
pep8 规范要求4个空格为语句块缩进。(table使用的话把制表符4个转换成空格,不然windows和liunx不一样。)
初学者(4个空格为语句块缩进)经常犯的错误是tab键和空间键混用,造成的缩进不一致。
凡是报错信息看到:IndentationError: unexpected indent ,就是表示缩进不一致。
判断何时需要缩进,首先要理解物理行和逻辑行
物理行:编辑器中显示的代码,每一行内容是一个物理行。
逻辑行:Python解释器对代码进行解释,一个语句是一个逻辑行。
age_of_principal=40
guess_age=int(input(">>>:\n"))
If guess_age==age_of_principal:
print("Yes,yougotit....")
else:
print("No,it'swrong.")
If ….else…要连着使用,中间不能出现不缩进的语句,4个物理行组成一个逻辑行
转载地址:http://jjncl.baihongyu.com/