Как не отображать () и ' - PullRequest
       0

Как не отображать () и '

0 голосов
/ 07 февраля 2020

Я запускаю файл с 2 функциями, он работает правильно, но при его запуске отображается следующее:

Enter your weight (in Kilograms): 81

Enter your height (in Centimeters): 175

Enter your age: 20

Enter 5 if you are a male or -161 if you are female: 5

('You have to consume between: ', 1447.0, 'and', 1537.4375, 'calories a day.')

Ниже мой код:

def calBurned(weight:float, height:float, age: int, genderValue:float)->str:
    TMB = (10*weight)+(6.25*height)-(5*age) + genderValue
    minCal = TMB*0.80
    maxCal = TMB*0.85
    text = "You have to consume between ", minCal, "and", maxCal, "calories a day."
    return text

def calRec()->None:
    weight = float(input("Enter the your weight (in Kilograms): " ))
    height = float(input("Enter your height (in Centimeters): "))
    age = int(input("Enter your age: "))
    genderValue = float(input("Enter 5 if you are a male or -161 if you are female: "))
    calRecom= calBurned(weight, height, age, genderValue)
    print(calRecom)

calRec()

Is можно вернуть только текст без всех (',')?

Ответы [ 4 ]

2 голосов
/ 07 февраля 2020

Ваш text является кортежем. Вы можете преобразовать каждый из его элементов в строку и вернуть их конкатенацию:

text = " ".join(map(str, text))

Вы также можете построить text в виде строки:

text = f"You have to consume between {minCal} and {maxCal} calories a day."

И последнее, но не менее важное: функция не должна возвращать форматированный текст; он должен вернуть результаты вычислений (minCal,maxCal). Форматирование должно быть сделано вызывающей стороной.

1 голос
/ 07 февраля 2020

Использование:

text = "You have to consume between {} and {} calories a day.".format(minCal, maxCal)
1 голос
/ 07 февраля 2020

В calcBurned вы не объединяли строки, скорее вы сделали это кортежем в этой строке:

text = "You have to consume between ", minCal, "and", maxCal, "calories a day."

измените все запятые (,) на плюсы (+) и измените minCal и maxCal до str(minCal) и str(maxCal), и оно должно работать:

text = "You have to consume between " + str(minCal) + " and " + str(maxCal) + " calories a day."
0 голосов
/ 07 февраля 2020

Просто измените

text = "You have to consume between ", minCal, "and", maxCal, "calories a day."

на

text = f"You have to consume between {minCal} and {maxCal} calories a day."
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...