首页 > 解决方案 > 将用户输入转换为更改输入持续时间的布尔值的时间?

问题描述

我正在研究这个项目游戏以更好地掌握 python。我试图让用户输入角色必须花费的时间,然后在完成输入的原始时间之前不允许用户做同样的事情。我已经尝试了一些方法,但我的菜鸟方式会产生不同的错误结果。(时间戳,将输入转换为 int 和不同位置的时间,timeDelta)

def Gold_mining():
    
    while P.notMining:
    

        print('Welcome to the Crystal mines kid.\nYou will be paid in gold for your labour,\nif lucky you may get some skill points or bonus finds...\nGoodluck in there.')
        print('How long do you wish to enter for?')
        time_mining = int(input("10 Gold Per hour. Max 8 hours --> "))
        



        
        if time_mining > 0 and time_mining <= 8:
            time_started = current_time
            print(f'You will spend {time_mining} hours digging in the mines.')
            P.gold += time_mining * 10
            print(P.gold)
            P.notMining = False
            
            End_Time = (current_time + timedelta(hours = 2))
            print(f'{End_Time} time you exit the mines...')
        elif time_mining > 8:
            print("You can't possibly mine for that long kid, go back and think about it.")
        else:
            print('Invalid') 

在设定的时间后,我希望它把 bool 值改回 false,这样你就可以再次挖矿了。

“Crystal Mining”被映射到另一个用于测试的键,所以我的输出显示“Inventory”,但当它正常工作时会显示“Crystal Mining”,目前看起来像这样:

  *** Page One ***  
Intro Page
02:15:05
  1 Character Stats
  2 Rename Character
  3 Inventory
  4 Change Element
  5 Menu
  6 Exit
Num: 3
Welcome to the Crystal mines kid.
You will be paid in gold for your labour,
if lucky you may get some skill points or bonus finds...
Goodluck in there.
How long do you wish to enter for?
10 Gold Per hour. Max 8 hours --> 1
You will spend 1 hours digging in the mines.
60
Traceback (most recent call last):
  File "H:\Python ideas\input_as_always.py", line 176, in <module>
    intro.pageInput()
  File "H:\Python ideas\input_as_always.py", line 45, in pageInput
    self.pageOptions[pInput]['entry']()
  File "H:\Python ideas\input_as_always.py", line 134, in Gold_mining
    End_Time = (current_time + timedelta(hours = 2))
TypeError: can only concatenate str (not "datetime.timedelta") to str

标签: python-3.x

解决方案


推荐阅读