repeating program (python)
Question: Heyho

I want to repeat a program after runnning a task. At the start of the program
I ask some questions, than the code jumps into the task. If the task is done
the questions sholud ask again.. Each Question reads some infos at the serial
port. If i get the infos ten times ich will restart the programm.. but the
window closes and i must start the file..

What can i do?

    
    
    import serial
    import struct
    import datetime
    
    print("\n")
    print("This tool reads the internal Bus ")
    print("-----------------------------------------------------------------------")
    COM=input("Check your COM Port an fill in with single semicolon (like: 'COM13' ): ")
    
    ser = serial.Serial(
        port=COM,
        baudrate=19200,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS
    )
    
            print("\n")
            print("Please choose an option: ")
            print("Polling of measured values and the operating status (1)")
            print("Reading Parameter memory (2)")
            print("Reading Fault memory (3)")
            print("EXIT (4)")
            print("\n")
            i=input("Reading: ")
            while(i==1):
                count=0
                while (count<10):
                        print(file.name)
                            print(ser.isOpen())
                            print ("connected to: "+ ser.portstr)
                            data = "\xE1\x14\x75\x81"
                            ser.write(data)
                            a=(map(hex,map(ord,ser.read(46))))  
    
                            with open("RS485_Reflex.txt",mode='a+') as file:
                            file.write(str(datetime.datetime.now()))
                            file.write(", Values: ,")
                                file.write(str(a))
                                    file.write("\n")
                            print(a)
                            count=count+1
    
                    else:
                        i=0
            loop=1
    #-----------------------------------------------------------------------
            while(i==2):
                    count=0
                    while (count<10):
                            print(file.name)
                            print(ser.isOpen())
                            print ("connected to: "+ ser.portstr)
                            data = "\xE1\x13\x00\x00\x74\x81"
                            ser.write(data)
                            a=(map(hex,map(ord,ser.read(11))))
    
                            with open("RS485_Reflex.txt",mode='a+') as file:
                                    file.write(str(datetime.datetime.now()))
                                    file.write(", Parameters: , ")
                                    file.write(str(a))
                                    file.write("\n")
                            print(a)
                            count=count+1
                    else:
                            i=0
    
    #---------------------------------------------------------------------
            while(i==3):
                    count=0
                    while (count<10):
                            print(file.name)
                            print(ser.isOpen())
                            print ("connected to: "+ ser.portstr)
                            data = "\xE1\x12\x00\x00\x73\x81"
                            ser.write(data)
                            a=(map(hex,map(ord,ser.read(11))))
    
                            with open("RS485_Reflex.txt",mode='a+') as file:
                                    file.write(str(datetime.datetime.now()))
                                    file.write(", Fault: , ")
                                    file.write(str(a))
                                    file.write("\n")
                            print(a)
                            count=count+1
                    else:
                            i=0
    
    #----------------------------------------------------------------------
            while(i==4):
                    file.close()
                    ser.close()
                    sys.exit(0)
    
            file.close()
            ser.close()
    


Answer: First, you should use **If/Elif/Else** statements instead of while loops.

    
    
    while(i==1):
    

should be

    
    
    if(i==1)0:
    

This is a simple program I wrote that you can follow:

    
    
    # setup a simple run_Task method (prints number of times through the loop already)
    def run_task(count):
        print "Ran a certain 'task'", count,"times"
    
    # count is a variable that will count the number of times we pass through a loop
    count = 0
    # loop through 10 times
    while(count<10):
        # ask questions
        q1 = raw_input("What is your name?")
        q2 = raw_input("What is your favorite color?")
    
        # run a 'task'
        run_task(count)
    

