How do I fix this program with value-returning functions? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How do I fix this program with value-returning functions?

gamefreak517gamefreak517 Posts: 1Member
import random

def main(get_playerchoice, get_compchoice):
play_again = 'yes'
while play_again == 'yes':
get_playerchoice = player_choice()
get_compchoice = computer_choice()
winner = winner_status(player_count, tie-count, computer_count)
play_again = raw_input ('Do you wanna play again? Enter yes or press any other key: ')
print 'Computer wins ', computer_count
print 'Player wins ', player_count
print 'tie ', tie_count

def get_compchoice():
computer_choice = random.randint(1, 3)
return computer_choice
def get_playerchoice():
player_choice = input ('Enter a number between 1 and 3: ')
while player_choice != (1,2,3):
print 'Number must be between 1 and 3'
player_choice = input ('Enter a number between 1 and 3: ')
return player__choice

def determine_winner(computer_choice, player_choice):
if computer_choice == 1 and player_choice == 2:
winner = player
if computer_choice == 2 and player_choice == 3:
winner = player
if computer_choice == 3 and player_choice == 1:
winner = player
winner = computer
winner = computer
winner = computer
return winner
def winner_status(winner):
if winner == player_count:
player_count += 1
if winner == computer_count:
computer_count += 1
if winner == tie_count:
tie_count += 1
return player_count, tie_count, computer_count


  • ebladeeblade Posts: 1Member
  • bubbatremellbubbatremell Posts: 39Member
    In get_playerchoice():
    -you try to return player__choice, instead of player_choice.
    -you have while player_choice != (1,2,3), but you expect player choice to be a string. input() returns strings, so player_choice will never be a tuple. You want something like player_choice in ('1','2','3')

    In determine_winner:
    -you have player_choice == 2, which will proly be false b/c player_choice is a string
    -your indentation of the cases for p_choice and c_choice is wrong. Those are nested loops, so you can't get to c_choice == 2 and p_choice == 3 UNLESS c_choice == 1 and p_choice == 2 are both true. You proly want those to be at the same indentation level as the first one.

    In winner_status:
    -you have return x, y, z. when this returns in main, winner be something like
    winner == (3,18,0) # the compy is beating you :P
    You go on to call computer_count, player_count, and tie_count, but they don't exist in main-- only a 3-tuple named 'winner'. Either access the values in winner (ie winner[0]...), or assign all the values (ie a, b, c = winner_status(winner)).
    Also, winner_status takes one argument. You are calling it with 3. This will break it.

    That's all the easy stuff to pick out. If there is more, or if there are logical problems, I defer to eblade.
Sign In or Register to comment.