[Tutor] Coding question: How to call a function when the function is assigned a variable

Alan Gauld alan.gauld at yahoo.co.uk
Mon Oct 23 03:15:54 EDT 2017

On 23/10/17 01:40, Historitical Show wrote:

> Here is what I have so far:
> import time
> coins = 0
> score = 0
> cptype = 1
> start = time.time()
> end = time.time()

Note that you are setting start and end to
almost identical times and never change
then later.

> def main():
>     global coins, score, cptype, start, end

Rather than declare the variables above then make
them global just define them inside the function.

>     if start - end <= 1 and coins == 20:
>         ...
>         time.sleep(5)
>     else:
>         start is True

I'm not sure what you think this does but in
fact it performs a test to see if start is
the literal value True - which it isn't,
it was set to time() So the test result will be
False. But you don't store the result you
throw it way. So the real effect of this line
is to do nothing except waste a tiny amount
of time.

Maybe you wanted to reassign the time here?

start = time.time()

>         type = input("")
>         if type == "":
>             score = score + 1
>             coins = coins + cptype
>         end is True

same as above, I suspect you really want
to set the end time.

>         print(start - end)
>         main()

While this will work it's not a good technique
for a potentially indefinitely repeating loop,
instead use a while.

In your case you wanted to read 20 values,
but you don't check that here. Why not use

while score < 20:
    your code here....
    ...without the call to main()

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list