Doppel Doppel - 4 months ago 11
Python Question

text game if check1 and check2 and check3 statment

So this is the beginning of a simple text game im working on.
When I run the program it will run through them multiple times before it can get to done. Not just one for each like I want. But I still want it to pick them at random, so I'm not sure if there is a better way to do this? Could someone tell me what I did wrong?

import random
from collections import defaultdict

Blist = ["hash browns", "eggs", "cheese", "sausage"]
Dictionary = {"a":1,"b":1,"c":1,"d":1}
Tup = (1,2,3,4,5)
check1 = False
check2 = False
check3 = False

def question():
answer = ((Blist), (Dictionary), (Tup))
new = (random.choice(answer))
print(new)
print("which is this?")
print("a List, Dictionary, or Tuple?")
x = str(input('what is it?\n'))
if check1 == True and check2 == True and check3 == True:
multiL()
else:
if x == 'list' and new == Blist:
global check1
check1 = True
print("that is correct, this is a list")
question()
if x == 'dictionary' and new == Dictionary:
global check2
check2 = True
print("that is correct, this is a dictonary")
question()
if x == 'tuple' and new == Tup:
global check3
check3 = True
print("that is correct, this is a tuple")
question()
if x == 're':
multiL()
else:
print("that is not one of the choices, try again?\n")
question()

def multiL():
print("done")

question()

Answer

You can move your first line within your function outside of the function. For now, make it a list:

awnser = [(Blist), (Dictionary), (Tup)]

def question():
...

Then choose one of them at random within the function:

def question():
    newawn = (random.choice(awnser))

When one is chosen, you can now delete the appropriate selection from the list to avoid repetition:

if x == 'list' and newawn == Blist:
       global check1
       check1 = True
       print("that is correct, this is a list")
       awnser.remove(Blist)
       question()

You can use .awnser.remove() to remove specific items from a list, such as in this case.