0

I'm making a very simple graphical program and am quite new.

nums = ["second", "third", "fourth"] for colours in range(3): numString = nums[colours] inputWindow.getMouse() colour1 = inputBox.getText() inputBox.setText("") instruction.setText("Please enter the {0} colour: ".format(numString)) 

Where I put 'colour1', I want it to cycle through colour1, colour2, colour3 and colour4 on each iteration (without using a long if statement). Dictionary cannot be used for this particular program.

At the end, the function returns all of these colours inputted from the user. I tried using a list but realised you can't use them for variable names.

Thanks for help.

Edit: Seeing as there's a lot of confusion (which I'm sorry for), I'll try and explain better: My code up there is kind of strange and comes from nowhere. I'll simplify it:

def getInputs(): for colours in range(3): colour1 = userInput() return colour1, colour2, colour3, colour4 

This is basically the gist. I wanna know if there's a way to cycle through the different variables where 'colour1 = userinput()' is (without using dictionary).

7
  • I read about this, but I'm doing a particular assignment and don't think I'm allowed to use dictionary :S Commented Nov 20, 2014 at 16:09
  • 1
    Ok, what are you allowed to use? I don't want to post an answer, only to be told "sorry, I'm not allowed to use that either" Commented Nov 20, 2014 at 16:11
  • @Jett, why would you not be allowed use a dict? Commented Nov 20, 2014 at 16:14
  • 2
    this example is really confusing, do as @PadraicCunningham advises and use a dictionary Commented Nov 20, 2014 at 16:21
  • Pretty much everything except dictionary I think. Oh well.. I'll try use some kind of while loop Commented Nov 20, 2014 at 16:23

1 Answer 1

1

Editing to reflect the new information. The principal thing to keep in mind here is that you can use sequence types (list, dict, etc.) to collect your results.

def get_inputs(): # a list to collect the inputs rval = [] for colours in range(4): # range(4) will walk through [0,1,2,3] # this will add to the end of the list rval.append(userInput()) # after the for loop ran four times, there will be four items in the list return rval 

In case you really want to want return a tuple, the last line can be return tuple(rval)

Sign up to request clarification or add additional context in comments.

3 Comments

should it be colours[num*s*[c]] = inputBox.getText() (emphasis mine)?
Thanks, I'll try tweaking this around and see if I can get any results.
I've just edited my OP, the first one has a lot of unnecessary code which shouldn't have been looked at, sorry about that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.