[Tutor] Counting iterations of a function

Michael Dowell michael.dowell297 at gmail.com
Thu Nov 30 11:26:20 EST 2017


Hello, I'm trying to code a magic 8-ball program and it needs to include a
counter for the number of questions asked. I'm having a hard time
implementing a count, and need a little help. I had tried to implement it
in the main() function, but that (naturally) led it to only counting '1'
for each question, for each question asked. Is there a way to count how
many times a give line of code is executed? For instance, every time the
oracle() function is run, a counter goes up. Thanks for any help.

Program:


#Michael Dowell
#11-26-27
#Capstone: Magic 8-ball

#import modules
import random
import turtle


#Give welcome message
def print1():

    print("Hello!")
    print("Thank you for using this Magic 8-Ball Simulator.")
    print("We understand that you have a choice in magic 8-ball
experiences,\n and that you have no choice in viewing mine.")
    print("Thank you")

#Define main function

def main():
    #Set local variables
    count=0          #Tried to intergrate count into main() function
    query=''

#Get question
    query=str(input("Please type in your question(press 'Enter' to exit):"))

    while query!='':
        oracle()
        main()
        break

#Determine fate (the error "Variable 'fate' is not defined" was quite
existential)
def oracle():

    fateList=["It is certain","It is decidedly so","Without a doubt","Yes –
definitely","You may rely on it","As I see it, yes","Most likely","Outlook
is good","Yes","Signs point to yes","Reply hazy, try again","Ask again
later","Better not tell you now","Cannot predict now","Concentrate and ask
again","Don’t count on it","My reply is no","My sources say no","Outlook
not so good","Very doubtful"]
    fate=fateList[random.randint(0,len(fateList))-1]

#Draw 8 ball/Refresh fate
    turtle.pensize(10)
    turtle.speed(7)
    turtle.hideturtle()
    turtle.goto(0,-200)
    turtle.fillcolor('black')
    turtle.begin_fill()
    turtle.circle(250)
    turtle.end_fill()
    turtle.goto(0,-50)
    turtle.fillcolor('blue')
    turtle.begin_fill()
    turtle.circle(150)
    turtle.end_fill()
    turtle.fillcolor('white')
    turtle.begin_fill()
    turtle.pendown()
    turtle.goto(130,150)
    turtle.goto(-130,150)
    turtle.goto(0,-50)
    turtle.end_fill()
    turtle.penup()
    turtle.goto(-75,125)
    x=turtle.pos()

    #Helvetica is the one true font
    turtle.write(fate,font=("Helvetica",12,"normal"))

#call functions
print1()
main()

#exit message

for count in range(20):
    print("Thanks for playing!")


More information about the Tutor mailing list