语法错误python infile

时间:2016-11-15 04:56:42

标签: python python-2.7 python-3.x

我正在尝试让这个程序打开文件并打印一张CD或DVD的列表以及它们欠的金额和客户名称,但是我打印客户名称时出现语法错误

这是我得到的语法错误 -

  

语法无效:文件c中的BWHW7.pyw,第25行,第37行:\ Users \ Benjaguin \ Desktop \ python \ BWHW7.pyw,第25行       print(customer_name,end =' \ t')

这是我用过的代码..

 CD_SPINDLE = 16.50
    DVD_SPINDLE = 21.75

    def main():
       cd_spindle_counter = 0
       dvd_spindle_counter = 0

        print("Guest Name \tSpindle Code\tAmount of Spindles\tAmount Due")
       print()

       try:
           infile = open('spindles.txt', 'r')

           customer_name = infile.readline()

           while customer_name != '':
               customer_name = customer_name.rstrip('\n')
               print(customer_name, end='\t')

               spindle_code = infile.readline()
               spindle_code = room_type.rstrip('\n')
               print(spindle_code, end='\t')

               spindle_amount = infile.readline()
               spindle_amount = int(spindle_amount)
               print(spindle_amount, end='\t')

               if spindle_code == "c" or spindle_Code == "C":
                   payment_due = spindle_amount * CD_SPINDLE
                   cd_spindle_counter += 1
               elif spindle_code == "d" or spinlde_code == "D":
                   payment_due = spindle_amount * DVD_SPINDLE
                   dvd_spindle_counter += 1
               else:
                   payment_due = 0

               total_spindle_payment += payment_due

               if payment_due ==0:
                   print('invalid code')
               else:
                 print('$', format(payment_due, '8,.2f'))

               guest_name = infile.readline()

           infile.close()

           print()
           print('total number of CD spindles sold:   ', cd_spindle_counter)
           print('total number of DVD spindles sold:    ',dvd_spindle_counter)
           print()
           print('total CD/DVD purchases:   ', end='')
           print('$', format(total_spindle_payment, ',.2f'), sep='')

       except IOError:
           print('an error occured trying to open or read spindle.txt')

    main()

1 个答案:

答案 0 :(得分:1)

我认为问题是由于缩进不一致造成的。我已经更正了(一直使用2的缩进),现在它正在工作:

CD_SPINDLE = 16.50
DVD_SPINDLE = 21.75

def main():
  cd_spindle_counter = 0
  dvd_spindle_counter = 0

  print("Guest Name \tSpindle Code\tAmount of Spindles\tAmount Due")
  print()
  try:
    infile = open('spindles.txt', 'r')

    customer_name = infile.readline()

    while customer_name != '':
      customer_name = customer_name.rstrip('\n')
      print(customer_name, end='\t')

      spindle_code = infile.readline()
      spindle_code = room_type.rstrip('\n')
      print(spindle_code, end='\t')

      spindle_amount = infile.readline()
      spindle_amount = int(spindle_amount)
      print(spindle_amount, end='\t')

      if spindle_code == "c" or spindle_Code == "C":
        payment_due = spindle_amount * CD_SPINDLE
        cd_spindle_counter += 1
      elif spindle_code == "d" or spinlde_code == "D":
        payment_due = spindle_amount * DVD_SPINDLE
        dvd_spindle_counter += 1
      else:
        payment_due = 0

      total_spindle_payment += payment_due

      if payment_due ==0:
        print('invalid code')
      else:
        print('$', format(payment_due, '8,.2f'))

      guest_name = infile.readline()

    infile.close()

    print()
    print('total number of CD spindles sold:   ', cd_spindle_counter)
    print('total number of DVD spindles sold:    ',dvd_spindle_counter)
    print()
    print('total CD/DVD purchases:   ', end='')
    print('$', format(total_spindle_payment, ',.2f'), sep='')

  except IOError:
    print('an error occured trying to open or read spindle.txt')

main()