Discuss / Python / 请问为什么if语句会报错?

请问为什么if语句会报错?

Topic source
>>> if age=18
  File "<stdin>", line 1
    if age=18
          ^
SyntaxError: invalid syntax

不理解哪里有问题。 但是输入age>=18或者输入age<=18都是没有问题的。

这个没有打冒号,但是加上冒号也还是报错

>>> if age=18:
  File "<stdin>", line 1
    if age=18:
          ^
SyntaxError: invalid syntax

独上寒江

#3 Created at ... [Delete] [Delete and Lock User]

在python中,=是赋值符号,要判断是否相等,应该使用==做判断运算 即

age=18
if age==18:
    print('u r 18')

谢谢!@独上寒江 不知道怎么站内回复.只能这样了吧...

交作业

s = weight / height
bmi = float(s)
if bmi < 18.5:
    print('过轻')
elif bmi < 25:
    print('正常')
elif bmi < 28:
    print('过重')
elif bmi <= 32:
    print('肥胖')
else:
    print('严重肥胖')

loveprruy

#6 Created at ... [Delete] [Delete and Lock User]
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math #导入数学库
height=17.5
weight=62
bmi=pow((weight/height),2) #调用数学库的平方函数
if bmi>32:
    print('严重肥胖')
elif bmi>=28:
    print('肥胖')
elif bmi>=25:
    print('过重')
elif bmi>=18.5:
    print('正常')
else :
    print('过轻')

  • 1

Reply