大学上了四年,第一次在csdn中写blog,这次学习的是python,第一次学,第一次写,算是给自己错误代码做个记录
1.python与c不同,if之后不用{},需要用:,然后和他是一个代码块的代码。要和他首行缩进一样的单位,一般是四格。例如
a=1
b=2
if a<b:
print("a is less than b")
print("a is definitely less than b")
print("not sure if a is less than b")
空了相同格数
2.特别注意空格问题
a=3
b=4
if a<b:
print("c is less than d")
elif c==d:
print("c is eauql to f")
else c>d:
print("c is greater than f")
报错
File “”, line 3
if a<b:
SyntaxError: invalid character in identifier
系找原因发现:
1.if a<b: 是用中文打的,不是用英文。
遇到 SyntaxError: invalid character in identifier考虑1.空格 2.中英文问题
else c>d:
报错:
invalid syntax
语法错误
当这个if clause完成后,else后边只有一种情况,不存在后边在加 c>d:,应该直接为else: python逻辑性很强,省去了人们在去思考其他情况的精力。
3.无elseif 只有elif 或者
else :
if
4.
expected an indented block
少代码缩进块
5,
File “”, line 9
else:
^SyntaxError: invalid syntax
name="YK"
height_m = 2
weight_kg = 90
bmi=weight_kg /(height_m ** 2)
print("bmi:")
print(bmi)
if (bmi>23):
print("YK is overweight")
else:
print("YK is normai")
错因:if else成对使用,并且在py中其一对一定要缩进一致