Python 我应该在代码中的哪一个位置设置断点?
每次我试着运行程序时,它总是告诉我中断是在循环之外,但我不知道我还能把中断放在哪里。还有什么好方法可以记住在循环结束时在何处放置中断?您的Python 我应该在代码中的哪一个位置设置断点?,python,loops,break,Python,Loops,Break,每次我试着运行程序时,它总是告诉我中断是在循环之外,但我不知道我还能把中断放在哪里。还有什么好方法可以记住在循环结束时在何处放置中断?您的break语句是if和elif语句的一部分,它们位于循环之外。Python对空格敏感。您应该将所有if和elif语句缩进循环内部。缩进错误。似乎您正试图打破while循环,但您的while循环在 print'Personal information, journal and more to come' while True: x = raw_inp
break
语句是if
和elif
语句的一部分,它们位于循环之外。Python对空格敏感。您应该将所有if
和elif
语句缩进循环内部。缩进错误。似乎您正试图打破while循环,但您的while循环在
print'Personal information, journal and more to come'
while True:
x = raw_input()
if x =="personal information":
print' Edward , Height: 5,10 , EYES: brown , STATE: IL TOWN: , SS:'
elif x =="journal":
print'would you like you open a journal or create a new one? open or create'
if x =='createfile':
name_of_file = raw_input("What is the name of the file: ")
completeName = "C:\\python\\" + name_of_file + ".txt"
file1 = open(completeName , "w")
toFile = raw_input("Write what you want into the field")
file1.write(toFile)
file1.close()
elif x =='openfile':
print'what file would you like to open'
y = raw_input()
read = open(y , 'r')
name = read.readline()
print (name)
break
声明
您必须修复if和elif语句的缩进,使它们位于while循环内,然后break语句才能工作。坦率地说:您的
break
在循环外。您的if语句不在while循环中
如果x=='createfile':
缩进,它将在while循环运行之后运行
我猜您希望重新嵌入代码,以便它们都包含在循环中。我还将if
更改为elif
,因为这在这里似乎更合适:
if x == 'createfile'
爱德华,我已经看过你之前的一些问题了,我必须问你。您是否已经学习过任何基本的python教程?缩进是python中一个非常基本的概念,您似乎在缩进方面遇到了很多麻烦。这是一个不错的问题。这是一个关于基本误解的基本问题,但它并不清楚或不恰当,所以为什么会被否决?我已经完成了一堂基本的python课程,但我在缩进方面仍然有很多问题可能教程不好。你有一个关于缩进的好教程吗?@JasonFruit:试着看看这里的样式指南:。我建议您使用IDE或类似vim的工具,这样您就可以设置自动缩进。我建议您从非常简单的脚本开始,缩进一些东西。。。有几次里面有ifs之类的。。。查看更改缩进时的情况:)
print 'Personal information, journal and more to come'
while True:
x = raw_input()
if x =="personal information":
print' Edward , Height: 5,10 , EYES: brown , STATE: IL TOWN: , SS:'
elif x =="journal":
print'would you like you open a journal or create a new one? open or create'
elif x =='createfile':
name_of_file = raw_input("What is the name of the file: ")
completeName = "C:\\python\\" + name_of_file + ".txt"
file1 = open(completeName , "w")
toFile = raw_input("Write what you want into the field")
file1.write(toFile)
file1.close()
elif x =='openfile':
print'what file would you like to open'
y = raw_input()
read = open(y , 'r')
name = read.readline()
print (name)
break