Repetition for command exercise in python

Asked

Viewed 236 times

0

The exercise is this one:

Marcílio and Aurélio are disputing the election for president of the Academic Center. To be the candidate must have more votes than his opponent, and also a number of votes greater than the total of white votes. Write a program that receives 100 students' votes and displays a message informing the name of the chosen. If there is no winner, a message that a new vote will be required.

I tried so:

for cont in range(4):

     votosAlunos = str.upper( input("digite seu voto:"))

    if(votosAlunos == " Marcílio "):
        votosM = votosM + 1

    elif(votosAlunos == " Aurélio "):
        votosA = votosA + 1

    elif(votosAlunos == "Branco"):

        votosB = votosB + 1


    if(votosM > votosA) and (votosM > votosB):
       print("Marcílio")
    elif(votosA > votosM) and (votosA > votosB):
       print(" Aurélio")
    else: 
       print(" Nova votação")

1 answer

1


Friend your code has some problems besides that is unused, your comparison of results has a space before the name and are still in lowercase, you just forgot you used the function str.upper(), then the comparison would always go wrong.

votosAlunos = []
votosM = 0
votosA = 0
votosB = 0

for k in range(0, 99):      # Para 100 votos

    votosAlunos.append(str.upper(input("digite seu voto:")))    #Anexa na lista os votos

    if(votosAlunos[k] == "MARCÍLIO"):

        votosM = votosM + 1

    elif(votosAlunos[k] == "AURÉLIO"):

        votosA = votosA + 1

    elif(votosAlunos[k] == "BRANCO"):

        votosB = votosB + 1


if(votosM > votosA) and (votosM > votosB):
    print("Marcílio")
elif(votosA > votosM) and (votosA > votosB):
    print("Aurélio")
else: 
    print("Nova votação")

Browser other questions tagged

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