私は一連の入れ子になったループを使ってペイ電卓プログラムを書いています。私は指定された日に働いた時間の入力が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)
これは私のためにエラーの全体の多くをスローし、私はそれを感謝し、試してみて、より多くの私のコードをモジュール化するためにこれを使用します。ありがとう:) – Brittany
ねえ、どのバージョンのPythonを使用していますか?私はこれを2.7でテストしましたが、それはうまくいっていましたが、おそらく3.xではそれが違います。私の例外はTypeErrorとValueErrorの違いです – Aquiles