Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Inheritance Score Method

Sylvia Pan
Sylvia Pan
7,093 Points

Where did I do wrong?

The editor didn't give me any clue and I run this class well on my Mac. Could someone tell me where is the problem?

game.py
class Game:
  def __init__(self):
    self.current_score = [0, 0]

  def score(self):
    player = input("1 or 2?")

    if player == 1:
      self.current_score[0] += 1
    elif player ==2:
      self.current_score[1] += 1
    else:
      self.score()

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Your method is going to get the player as an argument; you don't need to ask for it using input(). You do need to change your method so it'll take the player argument, though.

Sylvia Pan
Sylvia Pan
7,093 Points

Opps, I see, I was overthinking it.

#you could use a tuple for player
player = (1,2)
input1 = int(input('Enter either 1 for player 1 or 2 for player 2:'))

for  a_player in player:
if input1 == a_player:
    self.current_score +=1
else:
# do some stuff if not the correct input
Roman Kuzhelev
Roman Kuzhelev
7,853 Points

I suppose that the issue you are talking about is falling into an infinite loop in all cases (even in cases when the user's input is correct). The problem is that you are trying to compare a string value with an integer value in your conditional statements. For example, consider the following one:

if player == 1

Here the value of a 'player' variable is a string due to the fact, that it has been produced by the result of the 'input' function. Thus, comparison of a string and an integer will always result in a False.

To make this work as expected you should explicitly convert the string value that has been derived from an 'input' function to int. Consider the following replacement in your code:

player = int(input("1 or 2? "))

This should fix the problem of infinite repetitions.

Sylvia Pan
Sylvia Pan
7,093 Points

Yeah, that's correct. I forgot to convert string to int. Thanks a lot.