python - 预期的缩进块 -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,如果它们都不起作用,它将返回到同一行要求相同的输入?
解决方案
这应该工作,
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)。因此,我添加了一个虚拟打印语句,它现在工作正常,用您的适当逻辑替换打印语句,您就可以开始了。
推荐阅读
- laravel - 如何使用环境变量作为其他环境变量的参考?
- apache-spark - 如何在火花中读取pdf文件
- javascript - 如何使用 Angular 6 ngFor 将子元素显示为父元素
- javascript - 如何在 SVG 上绘制与给定图像数据形状完全相同的折线对象
- c++ - 创建 C++ 链接列表时出现编译错误。架构 x86_64 的未定义符号:
- python - 无法在 Python 3 中的 Selenium 上设置 Luminati 代理
- sql - 从特定组 [符号] 中删除特定行 [时间] 的 SQL 语法
- android - 如何使用 android 处理 ASP.NET post 方法?
- c# - 我可以将 MVC5 项目添加到现有(仍在运行)ASPX 项目中吗
- python - 列表索引超出范围 Openpyxl