2016-12-13 3 views
0

おそらく簡単な修正が、私は仕事に出て、私は関数内に関数を持っていますが、それらを一度にどのように分割するのですか?

基本的にこれは私が10進形式で日中働いてきた時間を計算するために作られた小さなプログラムがあるだけでカント、(私はそのようにそれをしなければならないbecasuse仕事用のタイムシート用)は完璧に機能しますが、いつでも再起動してプログラムを再起動する機能を追加することにしました。

Time within Timeを呼び出してみましたが、再起動してからやり直してから元に戻ります。私はまた、Timeを呼び出してRestartを呼び出すRestart関数を作成しようとしましたが、それも動作しませんでした。

私は彼らの両方から脱出してプログラムをもう一度呼びたいと思っていましたが、これは可能ですか?

def Time(): 
    clear() 
    def is_valid_hours(): 
     while True: 
      h=input("Hour: ") 
      if h=="restart": return "yes" #this line doesnt work (tried break) 
      try: 
       h=int(h) 
       return h 
      except: 
       print("Please enter numbers!") 
    def is_valid_Minutes(): 
     while True: 
      h=input("Minute: ") 
      if h=="restart": return "yes" # this is the other line (break tried) 
      try: 
       h=int(h) 
       return h 
      except: 
       print("Please enter numbers!") 
    print("Please use time in a 24hour clock format when using this calculator") 
    print("Please enter arrival time:") 
    arrivalTimeHours=is_valid_hours() 
    arrivalTimeMinutes=is_valid_Minutes() 

    print("Please enter length of lunch break: ") 
    lunchBreakHours=is_valid_hours() 
    lunchBreakMinutes=is_valid_Minutes() 

    print("Please enter desired leave time: ") 
    leaveTimeHours=is_valid_hours() 
    leaveTimeMinutes=is_valid_Minutes() 

    if arrivalTimeHours>0: 
     arrivalTimeHours=arrivalTimeHours*60 
    arrivalTime=arrivalTimeHours+arrivalTimeMinutes 
    if lunchBreakHours>0: 
     lunchBreakHours=lunchBreakHours*60 
    lunchBreak=lunchBreakHours+lunchBreakMinutes 
    if leaveTimeHours>0: 
     leaveTimeHours=leaveTimeHours*60 
    leaveTime=leaveTimeHours+leaveTimeMinutes 

    totalTimeMinutes=leaveTime-(arrivalTime+lunchBreak) 
    decimalTime=totalTimeMinutes/60 
    print("Your decimal time is "str(decimalTime)) 
    newTime=input("Would you like to do a new time?: ") 
    return newTime.lower() 

newTime=Time() 
while newTime=="yes" or newTime=="ye" or newTime=="y" or newTime=="yah" or newTime=="yeah" or newTime=="yh": 
    newTime=Time() 
input("Press enter to close") 

EDIT: 私はこのaswellをやってみました、それはどちらか動作しませんでした。

def Time(): 
    clear() 
    notQuitting=True 
    while notQuitting==True: 
     def is_valid_hours(): 
      while True: 
       h=input("Hour: ") 
       if h=="restart": 
        notQuitting=False 
        return "yes" 
       try: 
        h=int(h) 
        return h 
       except: 
        print("Please enter numbers!") 
     def is_valid_Minutes(): 
      while True: 
       m=input("Minute: ") 
       if m=="restart": 
        notQuitting=False 
        return "yes" 
       try: 
        m=int(m) 
        return m 
       except: 
        print("Please enter numbers!") 
     #rest of code 
+0

'h.strip()== '再起動' の場合:MyException'を上げますか? – jbndlr

+0

これは、必要以上に複雑になっています。これにネストされた関数定義は必要ありません。 [有効な応答が得られるまで入力を求める](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response)を参照してください。 –

答えて

1

この目的で独自のExceptionを使用することができます。あなたがそれらを捕まえないままにしておかないことを確かめてください。

import datetime 

class RestartTimeException(Exception): pass # If user requests restart 
class StopTimeException(Exception): pass # If user requests stop 

def take_time(): 
    def read_or_fail(prompt): 
     while True: # As long as input is not valid or user does not abort... 
      try: 
       i = input(prompt).strip() 
       if i == 'restart': # User wants to restart: raise. 
        raise RestartTimeException 
       elif i == 'stop': # User wants to abort: raise. 
        raise StopTimeException 
       # Split input into hours and minutes and parse integer values (validity check) 
       return tuple(int(p) for p in i.split(':')) 
      except (TypeError, ValueError): 
       # On parsing failure inform user and retry 
       print(' - Parsing error; re-requesting value.\n - (Type \'stop\' to abort or \'restart\' to start over.)') 

    # Read arrival time and make datetime object (ignore year, day, month) 
    h, m = read_or_fail('> Arrival time (H:M): ') 
    arr = datetime.datetime.strptime('{:d}:{:d}'.format(h, m), '%H:%M') 

    # Read lunch break duration and make timedelta object 
    h, m = read_or_fail('> Lunch duration (H:M): ') 
    lun = datetime.timedelta(hours=h, minutes=m) 

    # Read leaving time and make datetime object (ignore year, day, month) 
    h, m = read_or_fail('> Leaving time (H:M): ') 
    dep = datetime.datetime.strptime('{:d}:{:d}'.format(h, m), '%H:%M') 

    # Calculate time difference as timedelta 
    total = dep - arr - lun 
    # Calculate difference in minutes (object only yields seconds) 
    return total.seconds/60.0 

if __name__ == '__main__': 
    do_new = True 
    while do_new: # As long as the user wants to ... 
     try: 
      mins = take_time() # Get times, calculate difference 
      print(' - MINUTES OF WORK: {:.2f}'.format(mins)) 
      do_new = input('> New calculation? ').strip().lower().startswith('y') 
     except RestartTimeException: 
      # Catch: User requested restart. 
      print(' - Restart requested.') 
     except (StopTimeException, BaseException): 
      # Catch: User requested stop or hit Ctrl+C. 
      try: 
       print(' - Stop requested.') 
      except KeyboardInterrupt: 
       pass # Just for clean exit on BaseException/Ctrl+C 
      do_new = False 

出力例:

は、この例示的な実装を見てください

> Arrival time (H:M): 8:17 
> Lunch duration (H:M): as 
    - Parsing error; re-requesting value. 
    - (Type 'stop' to abort or 'restart' to start over.) 
> Lunch duration (H:M): 0:37 
> Leaving time (H:M): 16:48 
    - MINUTES OF WORK: 474.00 
> New calculation? n 
関連する問題