1

I am unsure as to why the following doesn't work:

def main():
    userInputs()
    print(firstColour)

def userInputs():
    validColours = ['red', 'green', 'blue', 'yellow', 'magenta','cyan']
    while True:
        firstColour = input('Enter the 1st colour: ')
        secondColour = input('Enter the 2nd colour: ')
        thirdColour = input('Enter the 3rd colour: ')
        fourthColour = input('Enter the 4th colour: ')
        if firstColour in validColours:
             if secondColour in validColours:
                if thirdColour in validColours:
                    if fourthColour in validColours:
                        break
        else:
            print('Invalid colours.Enter the colours again between red, green, blue, yellow, magenta, cyan')
        return firstColour, secondColour, thirdColour, fourthColour

I thought that if i called the main function, It would print whatever I entered as the firstColour?

9
  • give print(firstColor) before break Commented Dec 2, 2014 at 16:47
  • i need it to print without having to use print in my userInput function since I'm planning to use it for something else. I've just simplified my problem on here Commented Dec 2, 2014 at 16:49
  • then return firstColor or return True Commented Dec 2, 2014 at 16:50
  • The function is returning a value, you just aren't capturing it! Commented Dec 2, 2014 at 16:51
  • Also you need to outdent the return statement. Right now you've got it inside the while when you mean to have it after the while termiantes. Commented Dec 2, 2014 at 16:52

3 Answers 3

3

If you want to print your first colour, try the following:

def main():
    firstColour, secondColour, thirdColour, fourthColour = userInputs()
    print(firstColour)

When you return multiple values in python in a function, it packs them into whats called a "tuple" which is a list of values put simply. You have to "unpack" them in order to use them.

There is also what appears to be a logic error in your userInputs function. Your return function is indented too far which makes it always return after the first attempt, instead of retrying.

def userInputs():
    validColours = ['red', 'green', 'blue', 'yellow', 'magenta','cyan']
    while True:
        firstColour = input('Enter the 1st colour: ')
        secondColour = input('Enter the 2nd colour: ')
        thirdColour = input('Enter the 3rd colour: ')
        fourthColour = input('Enter the 4th colour: ')
        if firstColour in validColours:
             if secondColour in validColours:
                if thirdColour in validColours:
                    if fourthColour in validColours:
                        break
        else:
            print('Invalid colours.Enter the colours again between red, green, blue, yellow, magenta, cyan')
    return firstColour, secondColour, thirdColour, fourthColour
Sign up to request clarification or add additional context in comments.

Comments

0

In python, you are returning what is called a tuple. If you just want to return firstColour, you'd just need adjust your logic to assign foundColour with the last found colour and then return foundColour

More info on tuples: https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences

Comments

0

You aren't utilizing the values that you return:

return firstColour, secondColour, thirdColour, fourthColour

You are returning 4 variables, but not using them

userInputs()

Replace the above with something like this:

firstColour, secondColour, thirdColour, fourthColour = userInputs()

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.