Распечатать предложение на Python без 'или ", используя функцию печати

1

Ниже мой код, я работаю с Python2.7

#list of angle couples
phipsi=[[48.6,53.4],[-124.9,156.7],[-66.2,-30.8],[-58.8,-43.1], \
[-73.9,-40.6],[-53.7,-37.5],[-80.6,-16.0],[-68.5,135.0], \
[-64.9,-23.5],[-66.9,-45.5],[-69.6,-41.0],[-62.7,-37.5], \
[-68.2,-38.3],[-61.2,-49.1],[-59.7,-41.1],[-63.2,-48.5], \
[-65.5,-38.5],[-64.1,-40.7],[-63.6,-40.8],[-66.4,-44.5], \
[-56.0,-52.5],[-55.4,-44.6],[-58.6,-44.0],[-77.5,-39.1], \
[-91.7,-11.9],[48.6,53.4]]
#minimal deviation tolerated for the 1st number
a=-57-30
#maximal deviation tolerated for the 1st number
b=-57+30
#minimal deviation tolerated for the 2nd number
c=-47-30
#maximal deviation tolerated for the 2nd number
d=-47+30
i=0
#Check if the couple fit into the intervals of numbers
while i < len(phipsi):
    if phipsi[i][0]>a and phipsi[i][0]<b:
        if phipsi[i][1]>c and phipsi[i][1]<d:
            print ('the couple ', phipsi[i] ,' has his angles in helix')
    else:
        print ('the couple ', phipsi[i] ,' does not have his angles in helix')
    i=i+1

Это то, что я получаю

('the couple ', [-55.4, -44.6], ' has his angles in helix')
('the couple ', [-58.6, -44.0], ' has his angles in helix')
('the couple ', [-77.5, -39.1], ' has his angles in helix')
('the couple ', [-91.7, -11.9], ' does not have his angles in helix')
('the couple ', [48.6, 53.4], ' does not have his angles in helix')

Как я могу получить

the couple [-77.5, -39.1] has his angles in helix
the couple [-91.7, -11.9] does not have his angles in helix

Я проверил в разделе справки или с другими символами, но не мог понять это... Спасибо за вашу помощь

  • 2
    удалите скобку print 'the couple ', phipsi[i] ,' has his angles in helix'
Теги:
printing
python-2.7

2 ответа

1
Лучший ответ

Используйте форматирование строк.. Здесь вы идете:

while i < len(phipsi):
    if phipsi[i][0]>a and phipsi[i][0]<b:
        if phipsi[i][1]>c and phipsi[i][1]<d:
            print ('the couple [%+.2f, %+.2f] has his angles in helix' % (phipsi[i][0], phipsi[i][1]))
    else:
        print ('the couple [%+.2f, %+.2f] does not have his angles in helix' % (phipsi[i][0], phipsi[i][1]))
    i=i+1
6

В Python 2.x print - это не функция, а инструкция, и она не включает в себя парсеры вокруг списка печатных объектов. В Python 3.x print была изменена на функцию. Вы используете синтаксис Python 3.x.

У вас есть два варианта:

  1. Вместо этого используйте синтаксис Python 2.x, пропустив parens: print a, b, c
  2. Добавьте from __future__ import print_function чтобы отключить оператор печати и использовать функцию печати. Это позволяет использовать синтаксис Python 3.x в достаточно недавнем Python 2.x.

На данный момент вы печатаете один tuple, и вы видите на repr этого кортежа, то есть то, что вы получите с print((a,b,c)) с функцией Python 3.x печати.

Примечание. print автоматически добавляет пробелы, нет необходимости добавлять их в свои строки.

  • 0
    Эта печать была утверждением в Python 2.x - лишь одна из немногих ошибок.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню