Python 3.x (else,elif)的问题显示了一个错误
我开始学习Python。视频演示了如何制作一个简单的计算器。我重写了它,但它给了我一个错误(无效语法),并突出显示了第一个elif中的字母E。版本3.8.6。谢谢你的理解Python 3.x (else,elif)的问题显示了一个错误,python-3.x,Python 3.x,我开始学习Python。视频演示了如何制作一个简单的计算器。我重写了它,但它给了我一个错误(无效语法),并突出显示了第一个elif中的字母E。版本3.8.6。谢谢你的理解 # calculator what = input("What's up ?(+, -):") a = float(input('1 ч ')) b = float(input('2 ч ')) if what == '+' : c = a + b print('result' + str(c
# calculator
what = input("What's up ?(+, -):")
a = float(input('1 ч '))
b = float(input('2 ч '))
if what == '+' :
c = a + b
print('result' + str(c ))
elif what == '-' :
c = a - b
print('result' + str(c ))
else:
print('?')
我重写代码的视频(35:50时间)在这一行print('result'+str(c))
上,缺少缩进。这使Python认为除了if
语句之外没有别的东西了。然后,它看到一个elif
语句,这是意外的,因为如果没有if
语句,就不可能有elif
语句。因此,您的代码需要固定缩进:
# calculator
what = input("What's up ?(+, -):")
a = float(input('1 ч '))
b = float(input('2 ч '))
if what == '+' :
c = a + b
print('result' + str(c )) # fixed indentation
elif what == '-' :
c = a - b
print('result' + str(c )) # fixed indentation
else:
print('?')
您的print('result'+str(c))
语句没有缩进到if语句的主体中,因此elif不知道它应该引用什么。与另一个printf相同:需要缩进以使其成为elif的一部分。固定:
what = input("What's up ?(+, -):")
a = float(input('1 ч '))
b = float(input('2 ч '))
if what == '+' :
c = a + b
print('result' + str(c))
elif what == '-' :
c = a - b
print('result' + str(c))
else:
print('?')
缩进错误。请复习一些教程并学习基本的Python语法。。。缩进在Python中很重要,因此您的else
与任何if
都不匹配。。。托梅里库,非常感谢