0

I want to print out an error code "DAT_GRESKA" or "GRESKA" in other input and then make the code do nothing but in my case it is taking me back and asking me to redo the input because its false. How do I make it to stop but without using exit() or quit().

import csv


def unos_csv():
    ucsv = input("Unesi CSV datoteku: ")
    if ucsv == 'raspored1.csv' or ucsv == 'raspored2.csv':
        ucsv = str(ucsv)
        return ucsv
    else:
        print('DAT_GRESKA')
        return main()


def ime_predmeta():
    subname = input("Unesi kod predmeta: ")
    if subname.isupper():
        return subname
    else:
        print("GRESKA")
        return main()


def obrada():
    file = open(unos_csv(), "r")
    reader = csv.reader(file, delimiter=',')
    predmet = ime_predmeta()
    with open(predmet + '.txt', 'a')as a:
        for row in reader:
            danu_nedelji = int(row[0])
            dejan = row[3].split('[')[1].split(']')[0]
            if predmet in row[3]:
                t1 = row[1]
                t2 = row[2]
                h1, m1 = t1.split(':')
                h2, m2 = t2.split(':')
                t11 = int(h1) * 60 + int(m1)
                t22 = int(h2) * 60 + int(m2)
                tkon = t22 - t11
                tkon = str(tkon)
                if danu_nedelji == 0:
                    a.write("Monday" + ' ' + row[1] + ' ' + row[2] + ' ' + tkon + ' ' + dejan + '\n')
                elif danu_nedelji == 1:
                    a.write("Tuesday" + ' ' + row[1] + ' ' + row[2] + ' ' + tkon + ' ' + dejan + '\n')
                elif danu_nedelji == 2:
                    a.write("Wednesday" + ' ' + row[1] + ' ' + row[2] + ' ' + tkon + ' ' + dejan + '\n')
                elif danu_nedelji == 3:
                    a.write("Thursday" + ' ' + row[1] + ' ' + row[2] + ' ' + tkon + ' ' + dejan + '\n')
                elif danu_nedelji == 4:
                    a.write("Friday" + ' ' + row[1] + ' ' + row[2] + ' ' + tkon + ' ' + dejan + '\n')
    a.close()


def main():
    obrada()


if __name__ == '__main__':
    main()
 
  • I’m not sure what you believe `return main()` is doing. It does not return from the main function. Instead, it calls it once again, and returns the return value of main(). This will end in stack overflow if you repeat it enough times. Restructure your code and use exceptions to signal errors. – Lesiak Jan 14 '21 at 23:09

1 Answers1

1

I think you misunderstand how the return statement works.

The reason that your program "continues" ... it doesn't continue -- you specifically invoke your main program another time. If you simply want to go back to the calling location and continue, use

return

You used

return main()

which is a command to invoke main again, wait until it's done, and send that value back to whatever called the function.

Prune
  • 76,765
  • 14
  • 60
  • 81