首页 > 解决方案 > 预期的缩进块 -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 ')

我不断收到错误:

文件“Linear algebra.py”,第 37 行 while (selection3 == 'To-Punkters Hældning') and (ending != 'N') : ^ IndentationError: expected an indented block

我不明白的是,while selection3 == 'To-Punkters Hældning'-loop(我将把它称为循环 A)不应该与 while selection3 == 'Linjens Hældning'-循环(我将其称为循环 B)?

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

标签: pythonloops

解决方案


这应该工作,

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)块,它显然没有要执行的语句(意味着不完整的while block)。因此,我添加了一个虚拟打印语句,它现在工作正常,用您的适当逻辑替换打印语句,您就可以开始了。


推荐阅读