Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 12th, 2012, 02:38 PM
Jennifer.Hughes Jennifer.Hughes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 Jennifer.Hughes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 43 sec
Reputation Power: 0
Please help. I'm stuck!

We have an imaginary pile of 16 beans. I'm supposed to create two functions to represent two players in a game. Each function will be called upon by the main program to take their turn (take beans out of the pile). So far my main program is calling the functions, but it's not keeping up with the total number of beans after each player takes some out. It keeps going back to 16 so my program never finishes. Is my naming off? I'm very very very new to this and taking the course so I can get my degree in desktop support so please be patient with me, trust me I'm trying as hard as I can. Here is what I have so far. I just need it to keep the value after each turn instead of going back to 16.

def player_one(beans):
total= beans - 1
print("Player one takes 1 bean. Beans left:")
return total

def player_two(beans):
total = beans - 3
print("Player two takes 3 beans. Beans left:")
return total

beans = 16

while beans > 0:

print(player_one(beans))
if beans == 0:
print("Player one loses.")

print(player_two(beans))
if beans == 0:
print("Player two loses.")

else:
print("Game Over")

Reply With Quote
  #2  
Old November 12th, 2012, 10:08 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,809 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 18 h 4 m 34 sec
Reputation Power: 6112
Welcome to the forums. As you can see, the whitespace in your post has been destroyed. You should surround any code with [ CODE ] and [ /CODE ] tags (without the spaces) so that the whitespace is preserved.

Whatever language this is may not support the way you're using variables. You return the total from these functions but never use that value. For instance:


Code:
print(player_one(beans))
if beans == 0:
print("Player one loses.")
That should probably be something like:


Code:
beans = player_one(beans)
if beans == 0:
print("Player one loses.")
Formatted and indented properly, of course.

You also need to check to see if beans goes negative, and not let player 2 take more beans than are available.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #3  
Old November 16th, 2012, 04:45 PM
Jennifer.Hughes Jennifer.Hughes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 Jennifer.Hughes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 43 sec
Reputation Power: 0
Thank you for the tip about using coding to keep the white space, i appreciate it. I guess I'm just having a big problem understanding how to use the variables. I am taking an online class and basically attempting to teach myself how to do all of this, but i'm finding it hard to understand all the rules. I have yet another assignment that involves me rewriting the following so it shows the value of x instead of an error.

Code:
def add_one(number):
     x = 1
     number = number + x
     print(number)

#The following is then entered in IDLE
>>>add_one(5)
6
>>>x
NameError: name 'x' not defined


The above is from my instructor. I have rewritten it one way...

Code:
def add_one(number):
     return number + x

#Then I run the program and enter the following into IDLE
>>>x = 1
>>>add_one(5)
6
>>>x
1


but i feel that i've taken the easy way out by inserting x=1 into IDLE. I'm willing to use the first one as an answer in my assignment, but I'm responsible for providing 2 different ways to print x properly, and I can't (on my own) figure another way. Everything I try ends up with an error for x. If you or anyone else could explain (like you would to a complete idiot since that's what I'm feeling like right now lol) to me the rules for making the program receive the value of x. Again please excuse my rookie ways of explaining things, and thank you to all who have helped, or might help in the future.

~Jenny

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Please help. I'm stuck!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap