1

I mean I've set up a choicebox.

    msg = "What would you like to do first?"
title = "Economy v1.0"
choices = ["Check your bank balance", 
           "Check the value of the stocks", 
           "Check what stocks you own", 
           "Buy stocks", 
           "Sell Stocks", 
           "Move to the next day.", 
           "Exit"]
choice = eg.choicebox(msg, title, choices)

But now I'd like to know how I get it to do something when a choice is selected. The tutorial on the website didn't give much help, and all the things I've experimented with haven't worked.

I tried to do this first:

if choice == choice[0]:
    #do stuff
if choice == choice[1]:
    #do more stuff
...

When I selected a choice, nothing happened.

So then I tried this:

if choice == "Buy Stocks":
    #do stuff
if choice == "Sell Stocks":
    #do stuff
...

But that didn't work either. I have tried a range of other things, but I'm not entirely sure what the choicebox returns, and what variable it stores it in. Could someone help me out here?

agf
  • 171,228
  • 44
  • 289
  • 238
Michael Curry
  • 991
  • 8
  • 20

2 Answers2

2

What you're doing should be working. Either of those if statements should be fine, just remember that strings are case sensitive.

The easygui.choicesbox() is going to return a string to the variable you assign it to. In this case, it will ask for a selection, then stuff that value you selected into your variable choice. Remember that choicesbox always returns a string.

if you try choice in choices it should return True.

Zac Smith
  • 328
  • 1
  • 10
0

For anyone coming here and wondering: The problem here is, that he isn't calling the list correctly. He is calling choice[] instead of choices[]

if choice == choices[0]:
    #do stuff
if choice == choices[1]:
    #do more stuff
...

Doing it this way works.

ellis
  • 1