1

Hello I am extremely new to programming so bear with me. I am trying to write a program that asks you for a color out of three choices and then generates a random color of its own and tells you if you win or lose based on if your color matches the generated color. I can't seem to nail down my syntax and I keep getting errors that I am unfamiliar with. This is my program.

import random


def ranred():
    global color
    color = 1
    global color_out
    color_out = "purple"
    f_color(color, color_in, color_out)
    return color, colorq


def ranblack():
    global color
    color = 2
    global color_out
    color_out = "black"
    f_color(color, color_in, color_out)


def ranpurple():
    global color
    color = 3
    global color_out
    color_out = "purple"
    f_color(color, color_in, color_out)


def f_color(color, color_in, color_out):
    print (color_out)
    if color == color_in:
        print ("win")
    else:
        print ("lose")


def main():
    color_in = input("Purple, Black or Red? ")
    Purple = 1
    purple = 1
    Black = 2
    black = 2
    Red = 3
    red = 3
    randomcolor1 = random.randrange(0, 2)
    randomcolor2 = random.randrange(0, 2)
    if randomcolor1 == 1 and randomcolor2 == 1:
        ranred()
    if randomcolor1 == 2 and randomcolor2 == 2:
        ranblack()
    if randomcolor1 == 2 and randomcolor2 == 1:
        ranpurple()
    if randomcolor1 == 1 and randomcolor2 == 2:
        ranpurple()

main()
2
  • Currently "Duplication argument 'color' in function definition" Commented Feb 7, 2014 at 20:12
  • Okay thanks so much! I fixed that issue that was a simple typo by now when I go to run the new code It asks my input question and then just outputs nothing. Any advise? Commented Feb 7, 2014 at 20:17

3 Answers 3

2

Of the error you've explained:

def f_color(color,color,color_out):

Is taking two arguments color. You can only have one argument named color:

def f_color(color, color_out):
Sign up to request clarification or add additional context in comments.

Comments

1
import random   

def computer_color(colors):
    return random.choice(colors)

def player_color(colors):
    while True:
        color = input('Purple, black, or red? ').strip().lower()
        if color in colors:
            return color

def main():
    colors = ['purple', 'black', 'red']
    pc = player_color(colors)
    cc = computer_color(colors)

    if pc == cc:
        print('Win!')
    else:
        print('Lose.')

if __name__=="__main__":
    main()

.

  1. The global keyword is only needed if you intend to assign a new value to a global variable; if you are just reading or modifying (ie appending to a list) it is not needed.

  2. In any case, globals are usually a bad idea; they make it harder to trace what is happening in a program and lead to hard-to-diagnose errors. It is much better to pass arguments and return results explicitly.

  3. Having lots of almost-identical functions is usually a bad sign and means you should refactor your code.

  4. By convention, variables should be named in all lowercase; starting with a capital letter is reserved for classes.

Comments

1

When you pass parameters into a function, they have to have unique names.

def f_color(color,color,color_out)

See how you have two things called color? How does Python know what value goes with what name? This simply cannot be. So name it something like color and color2 or something more descriptive. Or take out the second color argument since it doesn't look like you need it.

Then you have to call the function appropriately as well, so you when you call it, you pass in only one color argument:

f_color(color,color_out)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.