所以我有一个小游戏设置,玩家可以通过玩游戏来赚钱/赚钱
import random
wallet = [0]
bank = [0]
while True:
action = input('Select Action')
if action == 'wallet':
print(f"You have {str(sum(wallet))}
in your wallet!")
elif action == 'work':
print('You have worked and earned
some money')
您将如何设置5秒的冷却时间(以最简单的方式)以使用该命令,并通过友好的消息通知用户冷却时间尚未到期。
谢谢
答案 0 :(得分:0)
最简单的方法是只使用time.sleep
import random
import time
wallet = [0]
bank = [0]
while True:
action = input('Select Action')
if action == 'wallet':
print(f"You have {str(sum(wallet))} in your wallet!")
elif action == 'work':
print('You have worked and earned some money')
#Cooldown logic
print('Starting to cool down')
print(time.time())
time.sleep(5)
print(time.time())
print('Done cooling down')
样本输出
Select Actionwork
You have worked and earned some money
Starting to cool down
1557317823.196852
1557317828.1970088
Done cooling down
Select Action
您可以看到Starting to cool down
和Done cooling down
字符串之间有5秒的差异