Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How do I fix this program with value-returning functions? Posted by gamefreak517 on 22 Oct 2009 at 5:23 PM
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: ')
else:
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
else:
winner = computer
else:
winner = computer
else:
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
Report
Re: How do I fix this program with value-returning functions? Posted by eblade on 28 Oct 2009 at 8:32 PM
fix it to do what?

Report
Re: How do I fix this program with value-returning functions? Posted by bubbatremell on 11 Nov 2009 at 6:36 PM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.