-1

Imagine a situation where users have 10 coins and have 40 second to do a task. I want to implement something like this: if total time taken by the user is 50, reduce the coin by 1, similary if it's 60, reduce 2 and so on... How should I implement this in python.

PS: In short, I wanna reduce coins for each 10 seconds elapsed after 40 seconds (for example)

1 Answers1

0

Python has a time library you can use. Something like this maybe.

import time

coins = 10


def user_task():
    start_time = time.time()
    # user completes task here
    end_time = time.time()
    time_taken = end_time - start_time

    return time_taken
   

total_time_taken = user_task()

if total_time_taken >= 50:
    coins = coins - 1
forgebench
  • 11
  • 4
  • that will only take 1 coin if time >=50, it will not take 2 coins for time >=60 or 3 coins for time >=70... – Sembei Norimaki Nov 30 '22 at 14:20
  • @SembeiNorimaki Correct, the original poster will need to modify it for additional measurements. I wasn't going to type all that out when it's self explanatory. I understood his question as having to do with the timing portion, rather than sets of if/else statements. – forgebench Nov 30 '22 at 17:01
  • well you don't really need if else statements. Just substract 40 and then divide the excess by 10. eg: time = 70 -> 70-40=30 -> 30/10=3 coins substracted. That's why I mentioned that your answer was missing that calculation that gives you the result – Sembei Norimaki Dec 01 '22 at 12:33