-1
name1 = {}
name2 = {}
result = {}
while True:
    response = input("who do you want to vote on?")
    if name1:
        name1 += 1
    if name2:
        name2 += 1
    if response == 'finished':

        print(result, "has won")
        break

I'm currently trying to count values together, and print the winning name when "finished" has been typed in. I tried to use greater then > and less then < but then I get a error saying input is an int (number).

khelwood
  • 55,782
  • 14
  • 81
  • 108

2 Answers2

2

Here is a modified version of your code that I think solves what you were trying to do

names = ['foo', 'bar']
counts = [0, 0]
while True:
    response = input("who do you want to vote on? ")
    if response == names[0]:
        counts[0] += 1
    elif response == names[1]:
        counts[1] += 1
        
    if response == 'finished':
        if counts[0] == counts[1]:
            print('tie')
        elif counts[0] > counts[1]:
            print(names[0], 'has won')
        else:
            print(names[1], 'has won')
        break

A couple example runs

who do you want to vote on? foo
who do you want to vote on? foo
who do you want to vote on? bar
who do you want to vote on? finished
foo has won

and

who do you want to vote on? foo
who do you want to vote on? bar
who do you want to vote on? finished
tie
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • thanks for your answer, how does the count [0,0] get the position of foo and bar? – Captaincode Dec 10 '20 at 17:09
  • 1
    it doesn't, for simplicity i just chose to make them the same length and indices. instead you could either use a `dict` to map the key to the count or use `names.index('foo')` to find the index to use in `counts` for example – Cory Kramer Dec 10 '20 at 17:13
1

Here is a solution using a dictionary for storing the counts:

names={'name1':0,'name2':0}

while True:
    response = input("who do you want to vote on?")
    if response == 'finished':
        if len(set(names.values()))==1:
            print('Both candidates are tied.')
            break
        print(max(names, key=names.get), "has won.")
        break
    try:
        names[response]+=1
    except KeyError:
        print(f"{response} is no candidate.")

Sample output:

who do you want to vote on?name1

who do you want to vote on?name2

who do you want to vote on?finished
Both players are tied.

Or:

who do you want to vote on?name1

who do you want to vote on?finished
name1 has won.
angelogro
  • 124
  • 8