简体   繁体   中英

Input and Output program for python

So I have a question that requires this The features : color,size,flesh and class are separated by spaces. Write a Python program that asks the user for the names of the input file (in this case animals.txt) and the output file (any name). The program reads in the lines of the input file, ignores comment lines (lines starting with #) and blank lines and computes and prints the answers to the following questions: Total number of animals? Total number of dangerous animals? Number of large animals that are safe? Number of animals that are brown and dangerous? Number of safe animals with red color or hard flesh?

So I finished the program and everything seems to be working but so far when I enter the code and initiate the program, everything works, no errors, nothing but no output file gets generated. I don't know what is wrong exactly but if someone could point me in the right direction it would be highly appreciated.

import os.path
endofprogram = False

try:
    filename1 = input("Enter the name of input file: ")
    filename2 = input("Enter the name of output file: ")
    while os.path.isfile(filename2):
        filename2 = input("File Exists! Enter new name for output file: ")
        infile = open(filename1, 'r')
        ofile = open(filename2, "w")
except IOError:
        print("Error reading file! Program ends here")
        endofprogram = True
        if (endofprogram == False):
            alist = []
            blist = []
            clist = []
            largesafe = 0
            dangerous = 0 
            browndangerous = 0
            redhard = 0        
            for line in infile:
                line = line.strip("\n")
                if (line != " ") and (line[0] != "#"):
                    colour, size, flesh, clas = line.split('\t')
                    alist = alist.append(colour)
                    animals = alist.count()

                while clas == "dangerous":
                    dangerous = dangerous + 1

                while size == "large" and clas == "safe":
                    largesafe = largesafe + 1

                while colour == "brown" and clas == "dangerous":
                    browndangerous = browndangerous + 1

                while colour == "red" and flesh == "hard":
                    redhard = redhard + 1

                ofile.write(print("Animals = \n", animals))
                ofile.write(print("Dangerous = \n", dangerous))
                ofile.write(print("Brown and dangerous = \n", browndangerous)) 
                ofile.write(print("Large and safe = \n", largesafe))
                ofile.write(print("Safe and red color or hard flesh= \n", redhard))


            infile.close()
            ofile.close()

Maybe you can remove the print inside ofile.write

ofile.write(print("Animals = \n", animals))

to

ofile.write("Animals = \n" + str(animals))

Your indentation has completely messed the program up. The biggest offender is this section:

except IOError:
    print("Error reading file! Program ends here")
    endofprogram = True
    if (endofprogram == False):

The if line will only ever be executed right after the endofprogram = True line, at which point endofprogram == False will be false, and so nothing in the if block — which include the rest of the program — will be executed. You need to dedent everything from the if onwards by one level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM