Python Forum
Getting errors when trying to run programs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting errors when trying to run programs
#9
(Apr-10-2020, 11:34 AM)DreamingInsanity Wrote:
(Apr-10-2020, 11:21 AM)whois1230 Wrote: https://imgur.com/a/6OPMRci
I managed to fix the 'indented block' problem, but now I'm getting this('break' outside loop):
https://imgur.com/a/FB8RvGQ

The code still isn't indented properly. The 'break' needs to be inside the while loop, so does the if statement, I assume. The sys.exit() should probably be indented to be under the 'except' block.

If you could paste the code here, it would be much easier to fix.
#!/usr/bin/python3
import sys
try:
# open file stream
    file = open(file_name, "w")
except IOError:
    print ("There was an error writing to", file_name)
sys.exit()
print ("Enter '", file_finish,)
print ("' When finished")
while file_text != file_finish:
    file_text = input("Enter text: ")
if file_text == file_finish:
# close the file
    file.close
break
file.write(file_text)
file.write("\n")
file.close()
file_name = input("Enter filename: ")
if len(file_name) == 0:
    print ("Next time please enter something")
sys.exit()
try:
    file = open(file_name, "r")
except IOError:
    print ("There was an error reading file")
sys.exit()
file_text = file.read()
file.close()
print (file_text)


https://imgur.com/a/Ky1VkWH
I indented the whole code, but I'm getting 'invalid syntax' on the line
elif opt in ("-i", "--ifile"):
#!/usr/bin/python3
import sys, getopt
def main(argv):
    inputfile = ''
    outputfile = ''
    try:
        opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
    except getopt.GetoptError:
            print ('test.py -i <inputfile> -o <outputfile>')
            sys.exit(2)
            for opt, arg in opts:
                if opt == '-h':
                    print ('test.py -i <inputfile> -o <outputfile>')
                    sys.exit()
            elif opt in ("-i", "--ifile"):
                        inputfile = arg
                        elif opt in ("-o", "--ofile"):
                            outputfile = arg
                            print ('Input file is "', inputfile)
                            print ('Output file is "', outputfile)
                            if __name__ == "__main__":
                                main(sys.argv[1:])
                                
Reply


Messages In This Thread
RE: Getting errors when trying to run programs - by whois1230 - Apr-10-2020, 11:54 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020