Friday 15 January 2010

python - How do I call upon an item from a list? -



python - How do I call upon an item from a list? -

what problem code? @ moment asking question name nil else? think calling upon items list; used string, think incorrect. can help me should do?

subs=["multiplication" , "addition" , "subtraction"] import random score=0 def addition_sub1(): a=random.randint(1,20) b=random.randint(1,20) question1=int(input("what is" +str(a)+ "+" +str(b)+ "")) c=(a+b) if question == c: print("correct!") score=score+1 else: print("incorrect!") homecoming score def subtraction_sub1(): d=random.randint(1,20) e=random.randint(1,20) question2=int(input("what is" +str(a)+ "+" +str(b)+ "")) f=(d+e) if question2 == f: print("correct!") score=score+1 else: print("incorrect!") homecoming score def multiplication_sub1(): g=random.randint(1,20) h=random.randint(1,20) question2=int(input("what is" +str(a)+ "+" +str(b)+ "")) i=(d+e) if question2 == i: print("correct!") score=score+1 else: print("incorrect!") homecoming score name=input("what name? ") print("welcome quiz " +name) in range(0,9): op=random.choice(subs) if op == str(0): multiplication_sub1() if op == str(1): addition_sub1() if op == str(2): subtraction_sub1()

random.choice homecoming random element list. code should be:

if op == "multiplication": multiplication_sub1() if op == "addition": addition_sub1() if op == "subtraction": subtraction_sub1()

however, i'd give few suggestions.

it's fine have variables same name in different functions. can phone call them a, b, , question in functions.

since 3 if conditions mutually exclusive, should replace 2 @ bottom elif.

python

No comments:

Post a Comment