#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep 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")
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep 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

IMN logo majestic logo threadwatch logo seochat tools logo