2016-04-06 21 views
0

私は一連の入れ子になったループを使ってペイ電卓プログラムを書いています。私は指定された日に働いた時間の入力が0から24の間であることを確認しなければならないということ以外はすべて機能します。これは私がそのセクションで持っているコードです、私は複数の異なるオプションを試しましたが、それらはすべてプログラムをクラッシュさせるか全く認識されません。どんな助けもありがとう!Python - 複数入力検証

これは、関連するコードです:

for x in range(0, weeks): 
     for y in days: 
      while True: 
       print ("Enter the number of hours for Week", x+1, y, ":") 
       try: 
        hours = int(input()) 
       except ValueError: 
        print ("Invalid: Enter a positive integer") 
        continue 
       else: 
        break; 
      if y == 'Saturday': 
       newRate = satRate 
      elif y == 'Sunday': 
       newRate = sunRate 
      else: 
       newRate = baseRate 

      rate += (hours * newRate) 

あなたはもっと広い外観を必要とする場合、これは全体のコードです:

baseRate = -1 
while baseRate < 1: 
    baseRate = float(input("Enter the base pay rate: ")) 
    if baseRate < 1: 
     print("Invalid: Enter a non-negative amount") 
satRate = baseRate * 1.5 
sunRate = baseRate * 2 
pay = 0 
rate = 0 
hours = -2 
weeks = -1 
while weeks < 1: 
    while True: 
     try: 
      weeks = int(input("Enter the number of weeks: ")) 
     except ValueError: 
      print("Invalid: Enter a positive integer") 
      continue 
     else: 
      break 
    if weeks < 1: 
     print("Invalid: Enter a positive integer") 

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] 

for x in range(0, weeks): 
    for y in days: 
     while True: 
      print ("Enter the number of hours for Week", x+1, y, ":") 
      try: 
       hours = int(input()) 
      except ValueError: 
       print ("Invalid: Enter a positive integer") 
       continue 
      else: 
       break; 
     if y == 'Saturday': 
      newRate = satRate 
     elif y == 'Sunday': 
      newRate = sunRate 
     else: 
      newRate = baseRate 

     rate += (hours * newRate) 
pay = (round(rate, 2)) 
av = pay/weeks 
average = round(av,2) 

print("Total pay is: ", pay) 
print("Average pay per week is: ", average) 

答えて

1

あなたのコードは、あなたがない限り(有効な時間の入力をチェックしません。それがクラッシュするなどの理由で取り外された)。ここでは、1つの方法:

try: 
    hours = int(input()) 
    while not 0 <= hours <= 24: 
       print ("Invalid input, hours must be between 0 and 24 inclusive.") 
       print ("Try again") 
       hours = int(input()) 
     except ValueError: 
      print ("Invalid: Enter a positive integer") 
      continue 
0

通常、アクションの関数を使用して、よりモジュラー化する方がよいでしょう。このコードは動作する可能性があり、それははるかに読みやすいです:

def get_baserate(): 
    rate = input('Base rate:') 
    try: 
     rate = float(rate) 
     if not rate > 0: 
      print 'Rate must be positive' 
      return get_baserate() 
     return rate 
    except TypeError: 
     print 'Rate must be a positive number' 
     return get_baserate() 

def get_weeks(): 
    weeks = input('Number of weeks:') 
    try: 
     weeks = int(weeks) 
     if not weeks > 0: 
      print 'A positive number must be entered' 
      return get_weeks() 
     return weeks + 1 
    except TypeError: 
     print 'An integer must be entered' 
     return get_weeks() 

def get_hours(week, day): 
    hours = input('Enter number of hours worked on %s of week %s:' % (day, week)) 
    try: 
     hours = int(hours) 
     if not 0 <= hours <= 24: 
      print 'A number between 0-24 must be entered' 
      return get_hours(day) 
     return hours 
    except TypeError: 
     print 'An integer must be entered' 
     return get_hours(day) 

def get_payday(rate, hours, day): 
    rate = 2*rate if day == 'Sunday' else 1.5*rate if day == 'Saturday' else rate 
    return hours*rate 

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] 

rate = get_baserate() 
weeks = get_weeks() 
total_payday = 0 
for week in range(1, weeks): 
    for day in days: 
     hours = get_hours(week, day) 
     total_payday += get_payday(rate, hours, day) 
print 'Your total pay is: %s' % total_payday 
+0

これは私のためにエラーの全体の多くをスローし、私はそれを感謝し、試してみて、より多くの私のコードをモジュール化するためにこれを使用します。ありがとう:) – Brittany

+0

ねえ、どのバージョンのPythonを使用していますか?私はこれを2.7でテストしましたが、それはうまくいっていましたが、おそらく3.xではそれが違います。私の例外はTypeErrorとValueErrorの違いです – Aquiles