Konan
Konan

Reputation: 15

How do I use the return statement with strings? - Python

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?

Upvotes: 1

Views: 76

Answers (3)

Andy
Andy

Reputation: 50640

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()

Upvotes: 0

beiller
beiller

Reputation: 3135

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

Upvotes: 3

Frank V
Frank V

Reputation: 25429

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

Upvotes: 0

Related Questions