如果循环首先重复if语句

时间:2016-01-05 12:52:53

标签: python loops python-3.x if-statement

我正在尝试创建一个连续的问题循环,以便在我的项目中处理我的nmea句子的所有计算。由于某种原因,只执行第一个if语句。我究竟做错了什么?我还是陌生的新手

   if command_type == "$GPGGA" or "GPGGA" or "GGA":
        #define the classes
        gps = GPS()
        createworkbook = CreateWorkbook()
        convertfile = ConvertFile()
        print_gps = PrintGPS()
        #do the deeds
        createworkbook.openworkbook(data)
        print_gps.process_gpgga_data(data)
        createworkbook.closeworkbook_gpgga(data) 
        convertfile.convert2csv(data)
        convertfile.convert2kml(data)
    if command_type == "$GPRMC" or "GPRMC" or "RMC":
        #define the classes
        gps = GPS()
        createworkbook = CreateWorkbook()
        convertfile = ConvertFile()
        print_gps = PrintGPS()
        #do the deeds
        createworkbook.openworkbook(data)
        print_gps.process_gprmc_data(data)
        createworkbook.closeworkbook_gprmc(data) 
        convertfile.convert2csv(data)
        convertfile.convert2kml(data)
    if command_type == "$GPGLL" or "GPGLL" or "GLL":
        #define the classes
        gps = GPS()
        createworkbook = CreateWorkbook()
        convertfile = ConvertFile()
        print_gps = PrintGPS()
        #do the deeds
        createworkbook.openworkbook(data)
        print_gps.process_gpgll_data(data)
        createworkbook.closeworkbook_gpgll(data) 
        convertfile.convert2csv(data)
        convertfile.convert2kml(data)
    if command_type == "$GPGSA" or "GPGSA" or "GSA":
        #define the classes
        gps = GPS()
        createworkbook = CreateWorkbook()
        convertfile = ConvertFile()
        print_gps = PrintGPS()
        #do the deeds
        createworkbook.openworkbook(data)
        print_gps.process_gpgsa_data(data)
        createworkbook.closeworkbook_gpgsa(data) 
        convertfile.convert2csv(data)  
    if command_type == "$GPVTG" or "GPVTG" or "VTG":
        print('Better check $GPRMC')
    else:
        print("Invalid type:", command_type)

    list_gps_commands(data)     
    wannalook = input('Want to look at another message or no?')
    if not wannalook.startswith('y'):
        keep_asking = False
        print('********************')
        print('**mischief managed**')
        print('********************')

2 个答案:

答案 0 :(得分:2)

而不是:

 if command_type == "$GPGGA" or "GPGGA" or "GGA":

使用:

  if command_type == "$GPGGA" or command_type == "GPGGA" or command_type == "GGA":

或:

if command_type in ["$GPGGA", "GPGGA", "GGA"]:

答案 1 :(得分:2)

if command_type == "$GPGGA" or "GPGGA" or "GGA":

如您所见,此处您不会尝试检查 command_type 是否值为“$ GPGGA”或“GPGGA”或“GGA”。但是,如果 command_type ==“$ GPGGA”为真 “GPGGA”为真 “ GGA“是真的。

python中的非空字符串始终为true:您的第一个条件将被评估为true。

所以你必须这样做:

if command_type == "$GPGGA" or command_type == "GPGGA" or command_type == "GGA"