Python -->Valueerror: incomplete format

Asked

Viewed 590 times

2

Can someone help me with this code?
I have two problems: The first is the error mentioned in the title and the second is the vector of the function, which does not receive attribution (when I went to debug the compiler, I realized that U was not receiving any values).
I’m new to programming, don’t be surprised if there’s some barbaric error ;)

def calPercent(nvJ,tV):
    U = [0]*23
    for i in range(23):
        if nvJ[i]!= 0:
            U[i] = nvJ[i]/tV*100
    return U

V = [0]*23
P = [0]*23

voto = int(input("Numero do jogador (0=fim): "))
while voto!=0 :
    if voto<1 or voto>23 :
        print "Informe um valor entre 1 e 23 ou 0 para sair!"
    else:
        V[voto-1]+=1
    voto = int(input("Numero do jogador (0=fim): "))

P = calPercent(V,sum(V))

print "Foram computados",sum(V),"votos"

print "Jogador,Votos,%"
for i in range(23):
    if V[i]!=0 :
        print "%d,%d,%d%" %(i+1,V[i],P[i])

for i in range(23):
    if V[i] == max(V):
        print "O melhor jogador foi o numero %d, com %d votos, correspondendo a %d% do total de votos" %(i+1,V[i],P[i])
  • 1

    Since you debugged, can you report the error line? As for barbaric errors, I would only recommend that you give descriptive names to their variables. No U, V, nvJ etc.

1 answer

1


The "Valueerror incomplete format" error is due to the fact that Python did not recognize a replacement sequence starting with % at some point where you used the operator % for substitution.

If you look at your error message, Python always tells you the number of the line where it happened - this prevents you, or anyone else, from having to stare at the entire program to know where an error occurred. In this case, as the program is small, you can notice that the problem is in the last line - when trying to use the % of percentage as himself, in the stretch correspondendo a %d% do total - gets a % loose that generates the error. If you need to use a % separate a string with this type of formatting, put the sign twice in a row - this causes one of them to be kept after the substitution - that is, write the same passage as correspondendo a %d%% do total.

As for the values of U - the problem is that you are using Python 2 (and not Python 3 which has several significant language improvements) - and in Python 2, by default, uam division between integers always returns an integer - which means that in the line U[i] = nvJ[i]/tV*100 the division value shall always be "0", not a coefficient as expected.

The point way to solve this is to explicitly convert one of the split values to float, for example by writing: U[i] = nvJ[i] / float(tV) * 100. To avoid the same problem elsewhere, you can also tell Python to treat the division as it happens in Python 3, where inaccurate divisions between integers always generate float values - for this, put next to the first line of your program the directive from __future__ import division.

(the first line may be the Unix executable indicator: #! /usr/bin/env python, the following (first or second), the font coding newsletter, so that you can write accented characters without it being a syntax error: # coding: utf-8 )

  • Well observed! Now, about # coding: utf-8 I had already tried other times, but still does not recognize some symbols, as accents.

  • Why you are using incompatible encodings between your text editor, and the environment in which you run the script. Something common in windows. (And Mac and Linux is all UTF-8) - Anyway, it’s always good to read this article here about Unicode to understand things:http://local.joelonsoftware.com/wiki/O_M%C3%Adnimo_absoluto_que_todos_programadores_de_software_precise,_Absolutely,Positivamente_de_Saber_Sobre_Unicode_e_Conjuntos_de_Caracteres(Apologies!)

  • On the first line, the shebang should not be glued to the bar? #!/usr/bin/env python would be like, no?

  • https://answall.com/a/190895/64969

  • I just saw shebang in its format: https://answall.com/q/57702/64969; I had always imagined that this was heresy, that soon after the shebang should be put immediately the interpreter, with only the possibility of a single space on the line

  • are two bytes: #!, followed by a shell command line. env makes sure that environmental variables are respected to find the best python,and so the program can work inside a virtualenv, for example.

Show 1 more comment

Browser other questions tagged

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