Python - making my code more economic -


i need on making code more economical - sure that, there lot of lines can cut down on.

the code quiz ask 10 questions , score outputted @ end.

import random studentname=input("what name?:") score=0 trueanswer=0  def question():     global operation     global number1     global number2     global studentanswer     global score     number1=random.randrange(1,10)     number2=random.randrange(1,10)     operation=random.choice(["*","-","+"])     print("what is", number1,operation,number2,"?:")     studentanswer=int(input("insert answer:"))  def checking():     global score     if operation == "*":         trueanswer = number1*number2         if studentanswer == trueanswer:             print("correct")             score=score+1         else:             print("incorrect")             score=score     elif operation == "-":         trueanswer = number1-number2         if studentanswer == trueanswer:             print("correct")             score=score+1         else:             print("incorrect")             score=score     elif operation == "+":         trueanswer = number1+number2         if studentanswer == trueanswer:             print("correct")             score = score+1         else:            print("incorrect")            score=score  def main():     in range (10):         question()         checking()     print("your score is", score)  main() 

you can reduce entire checking function this:

def checking():     trueanswer = eval(str(number1)+operation+str(number2))     if studentanswer == trueanswer:         print("correct")         score=score+1     else:         print("incorrect")         score=score 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -