How to join the score with the last word on the left?

Asked

Viewed 65 times

1

It may sound like a silly question, but I really didn’t find an answer in my readings. I created a program whose purpose is to calculate my Coefficient of Performance and the percentage of the course completed in college, whose code follows below:

x = open('carto.txt')
cred = []
nota = []
sit = []
cr = 0
sumcred = 0
totcred = 244 #Total de créditos do curso.
aux = 0
for line in x:
    a = line.split(" ")
    cred += [int(a[0])]
    nota += [float(a[1])]
    sit += [(a[2])]
for i in range(len(nota)):
    sit[i] = sit[i].strip()
    if sit[i] not in {"Isento"}:
        sumcred += (cred[i]) #Calcula o somatório dos créditos cursados até o momento.
for i in range(len(nota)):
    sit[i] = sit[i].strip()
    if sit[i] in {"aprovado", "Isento"}:
        aux += (cred[i]) #Calcula o somatório dos créditos nas matérias onde se obteve aprovação ou isenção.
for i in range(len(cred)):
    sit[i] = sit[i].strip()
    if sit[i] not in {"Isento"}:
        cr += cred[i]*nota[i] #Faz a multiplicação da nota obtida em cada disciplina pela quantidade de créditos (desconsidera isenções).
print("O seu coeficiente de rendimento acumulado (CR) é igual a:",(round((cr/sumcred),2)))
print("O total de créditos do seu curso é de:",totcred,"créditos")
print("O total de créditos cursados até o momento é de:",aux,"créditos")
b = aux/totcred #Calcula o percentual concluído do curso.
print("O percentual concluído até o momento é de:",(round((b*100),2)),"%")
if b == 1:
    print("Parabéns, você concluiu o seu curso!")

The program runs correctly and provides the output shown in the image below:

inserir a descrição da imagem aqui

Note that in the last line, where it is written O percentual concluído até o momento é de: 53.69 % the percentage symbol (%) is separated from the number on the left.

Question: How do I join the % with the 53.69?

3 answers

5


This separation that you see with spaces happens because the print was done by separating each value with comma, ie by invoking print with several values:

print("O percentual concluído até o momento é de:",(round((b*100),2)),"%")
#                                                 ^--                ^--

To control the printing exactly the ideal is to interpolate the values in the final string. You can do this using format thus:

print("O percentual concluído até o momento é de:{}%".format(round(b*100,2)))

Note that the value is placed in the place where you have the {}. So you can build the string with as many values as you want and in the places you want.

You can also use f-string if you are working with python 3.6+:

print(f"O percentual concluído até o momento é de:{round(b*100,2)}%")

2

Change the line:

print("O percentual concluído até o momento é de:",(round((b*100),2)),"%")

for:

print("O percentual concluído até o momento é de:",str(round((b*100),2))+"%")

0

I assigned the variable and did cast.

I tested it like this and it worked.

a="Teste com valor d:"

b=round((17.45*4.987),2)

c=str(b)

d=c + "%"

print (a,d)

Teste com valor d: 87.02%

Maybe it’ll help you.

  • 2

    Thank you for your contribution to stackoverflow. However, Python is a tongue that allows several ways to compose a string with interpolation of values, and even the print function allows control of the spacing between items - Your proposal so, although it works, is not the best way to solve this specific problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.