应为缩进块-Python嵌套循环



我有这个代码:

import matplotlib

loopski = 1
while loopski == 1:
ending = 0
selection1 = input('Hvilket niveau? ')
if selection1 == 'a':
selectionA1 = input('Hvilket emne?')

if selection1 == 'b':   
selection2 = input('Hvilket emne? ')
if selection2 == 'Linear Algebra':
selection3 = input('Hvilken formel? ')
elif selection2 == 'Hovedmenu':
loopski = 2
selection3 = 'null'

while (selection3 == 'Linjens Hældning') and (ending != 'N'):
selection4 = input('Hvilken formel?' )
if selection4 == '1':
x1 = int(input('Angiv x1 '))
y1 = int(input('Angiv y1 '))
x2 = int(input('Angiv x2 '))
y2 = int(input('Angiv y2 '))

a = (x2-x1)/(y2-y1) 
b = y2 - (a*x2)
print('Din ligninger er: Y = {}x + {b}'.format(a,b))
elif selection4 == '2':
print('lol')
elif selection4 == '3':


while (selection3 == 'To-Punkters Hældning') and (ending != 'N') :
x1 = int(input('Definer x1 '))
y1 = int(input('Definer y1 '))
x2 = int(input('Definer x2 '))
y2 = int(input('definer y2 '))
aFun = (y2-y1)/(x2-x1)
a = aFun
b = y1 - (a*x1)
print('Formlen for de to punkter er: {}x + {}'.format(a,b)) 
ending = input('Tilfreds? Y(es)/N(o) ')
if ending == 'Y':
print('Perfekt :)')
loopski = 2
break
elif ending == 'N':
print('Du vil nu bive bragt tilbage til hovedmenu')
else:
print('ugyldigt svar')
ending = input('Tilfreds? Y/N ')

我一直收到错误:

文件"线性代数.py";,第37行while(selection3==‘To Punkters Hældning’(和(ending!=‘N’(:^缩进错误:应为缩进块

我不明白的是,当selection3=="To Punkters Hældning"-循环(我将把它称为循环A(时,不应该在同一个"上吗;电平";而selection3=="Linjens Hældning"-循环(我将把它称为循环B(?

我认为这是可行的,选择3将/不会满足循环B的要求,然后它将继续循环A,如果它们都不起作用,它将返回到同一行,要求相同的输入?

这应该可以工作,

import matplotlib

loopski = 1
while loopski == 1:
ending = 0
selection1 = input('Hvilket niveau? ')
if selection1 == 'a':
selectionA1 = input('Hvilket emne?')

if selection1 == 'b':   
selection2 = input('Hvilket emne? ')
if selection2 == 'Linear Algebra':
selection3 = input('Hvilken formel? ')
elif selection2 == 'Hovedmenu':
loopski = 2
selection3 = 'null'

while(selection3 == 'Linjens Hældning' and ending != 'N'):
selection4 = input('Hvilken formel?' )
if selection4 == '1':
x1 = int(input('Angiv x1 '))
y1 = int(input('Angiv y1 '))
x2 = int(input('Angiv x2 '))
y2 = int(input('Angiv y2 '))

a = (x2-x1)/(y2-y1) 
b = y2 - (a*x2)
print('Din ligninger er: Y = {}x + {b}'.format(a,b))
elif selection4 == '2':
print('lol')
elif selection4 == '3':
print("selection4=3") #replace with your code block/logic here.



while(selection3 == 'To-Punkters Hældning' and ending != 'N'):
x1 = int(input('Definer x1 '))
y1 = int(input('Definer y1 '))
x2 = int(input('Definer x2 '))
y2 = int(input('definer y2 '))
aFun = (y2-y1)/(x2-x1)
a = aFun
b = y1 - (a*x1)
print('Formlen for de to punkter er: {}x + {}'.format(a,b)) 
ending = input('Tilfreds? Y(es)/N(o) ')
if ending == 'Y':
print('Perfekt :)')
loopski = 2
break
elif ending == 'N':
print('Du vil nu bive bragt tilbage til hovedmenu')
else:
print('ugyldigt svar')
ending = input('Tilfreds? Y/N ')

这个问题与缩进无关(尽管在给定的代码中修复了一些缩进问题(,这里的问题是elif(selection4==3)块,它显然没有要执行的语句(意味着块时不完整的(。因此,我添加了一个伪print语句,现在它运行良好,用适当的逻辑替换print语句,就可以开始了。

试试这个:

import matplotlib

loopski = 1
while loopski == 1:
ending = 0
selection1 = input('Hvilket niveau? ')
if selection1 == 'a':
selectionA1 = input('Hvilket emne?')

if selection1 == 'b':   
selection2 = input('Hvilket emne? ')
if selection2 == 'Linear Algebra':
selection3 = input('Hvilken formel? ')
elif selection2 == 'Hovedmenu':
loopski = 2
selection3 = 'null'

while (selection3 == 'Linjens Hældning') and (ending != 'N'):
selection4 = input('Hvilken formel?' )
if selection4 == '1':
x1 = int(input('Angiv x1 '))
y1 = int(input('Angiv y1 '))
x2 = int(input('Angiv x2 '))
y2 = int(input('Angiv y2 '))

a = (x2-x1)/(y2-y1) 
b = y2 - (a*x2)
print('Din ligninger er: Y = {}x + {b}'.format(a,b))
elif selection4 == '2':
print('lol')
elif selection4 == '3':


while (selection3 == 'To-Punkters Hældning') and (ending != 'N') :
x1 = int(input('Definer x1 '))
y1 = int(input('Definer y1 '))
x2 = int(input('Definer x2 '))
y2 = int(input('definer y2 '))
aFun = (y2-y1)/(x2-x1)
a = aFun
b = y1 - (a*x1)
print('Formlen for de to punkter er: {}x + {}'.format(a,b)) 
ending = input('Tilfreds? Y(es)/N(o) ')
if ending == 'Y':
print('Perfekt :)')
loopski = 2
break
elif ending == 'N':
print('Du vil nu bive bragt tilbage til hovedmenu')
else:
print('ugyldigt svar')
ending = input('Tilfreds? Y/N ')

直接在while循环之后和if语句之前的行需要再次缩进。

最新更新