Python --ValueError: incomplete format

1

Can anyone help me with this code? I have two problems: The first is the error mentioned in the title and the second is the function vector, which does not receive assignment (when I was debugging in the compiler, I realized that U was not receiving any values). I'm new to programming, do not 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])
    
asked by anonymous 14.05.2016 / 01:45

1 answer

1

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

If you look at your error message, Python always informs the number of the line where it happened - this prevents you, or anyone else, from having to look at the entire program to find out where an error occurred. In this case, because the program is small, you can see that the problem is in the last line - when trying to use% % as itself, in the correspondendo a %d% do total stretch - gets a % loose that generates the error. If you need to use a % in a string with this type of formatting, sign the string twice in succession - this causes one of them to be retained after the substitution - that is, write the same excerpt as correspondendo a %d%% do total . / p>

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

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

(the first line can be the Unix executable code: #! /usr/bin/env python , the next (first or second), the code encoding of source code characters, so you can write accented characters without this being a syntax error: # coding: utf-8 )

    
14.05.2016 / 09:13