我怎样才能让我的 for 循环从它在 try 和 except 块中停止的地方继续? Python 3
How can i make my for loop continue from where it left off in the try and except block? Python 3
我希望我的程序在用户未输入数字时捕获错误。如果用户输入一个字母或只是按下 'enter',程序会再次从头开始执行 for 循环。我怎样才能让它从输入错误的地方开始?
#This is the size of the array
YEAR_SIZE = 12
months = [] ###This is the array that will hold the rainfall for each month
monthNames=['January','February','March','April','May',
'June','July','August','September',
'October','November','December']
def getMonthlyRainfall():
while True:
try:
total = 0
for month in range(YEAR_SIZE): ###This loop iterates 12 times for 12 entries
print ("Enter the rainfall for",monthNames[month], "in inches")
months.append(float(input()))
continue
except:
print ("Try again")
您可以使用另一个变量来跟踪用户给出的答案:
#This is the size of the array
YEAR_SIZE = 12
months = [] ###This is the array that will hold the rainfall for each month
monthNames=['January','February','March','April','May',
'June','July','August','September',
'October','November','December']
def getMonthlyRainfall():
ANSWERS = 0
while True:
try:
total = 0
for month in range(ANSWERS, YEAR_SIZE): ###This loop iterates 12 times for 12 entries
print ("Enter the rainfall for",monthNames[month], "in inches")
x = input()
months.append(float(x))
ANSWERS = ANSWERS + 1
except:
print ("Try again")
getMonthlyRainfall()
在这种情况下ANSWERS
YEAR_SIZE = 12
months = [] ###This is the array that will hold the rainfall for each month
monthNames=['January','February','March','April','May',
'June','July','August','September',
'October','November','December']
def getMonthlyRainfall():
while True:
total = 0
for month in range(YEAR_SIZE): ###This loop iterates 12 times for 12 entries1
try:
tmp = get_input(month)
except ValueError:
print ("Enter the rainfall for",monthNames[month], "in inches")
tmp = get_input()
months.append(tmp)
continue
def get_input(month):
try:
print ("Enter the rainfall for",monthNames[month], "in inches")
tmp = float(input())
except ValueError:
get_input(month)
这是一个更简洁的答案:
import calendar
def ask_for_rainfall(month_name):
while True:
try:
return float(input("Enter the rainfall for %s in inches" % month_name))
except:
print('Try again')
def get_monthly_rain_fall():
month_names = list(calendar.month_name[1:])
return {m_name: ask_for_rainfall(m_name) for m_name in month_names}
# Now you can do
# rain_falls = get_monthly_rain_fall()
# print(rain_falls["January"])
我希望我的程序在用户未输入数字时捕获错误。如果用户输入一个字母或只是按下 'enter',程序会再次从头开始执行 for 循环。我怎样才能让它从输入错误的地方开始?
#This is the size of the array
YEAR_SIZE = 12
months = [] ###This is the array that will hold the rainfall for each month
monthNames=['January','February','March','April','May',
'June','July','August','September',
'October','November','December']
def getMonthlyRainfall():
while True:
try:
total = 0
for month in range(YEAR_SIZE): ###This loop iterates 12 times for 12 entries
print ("Enter the rainfall for",monthNames[month], "in inches")
months.append(float(input()))
continue
except:
print ("Try again")
您可以使用另一个变量来跟踪用户给出的答案:
#This is the size of the array
YEAR_SIZE = 12
months = [] ###This is the array that will hold the rainfall for each month
monthNames=['January','February','March','April','May',
'June','July','August','September',
'October','November','December']
def getMonthlyRainfall():
ANSWERS = 0
while True:
try:
total = 0
for month in range(ANSWERS, YEAR_SIZE): ###This loop iterates 12 times for 12 entries
print ("Enter the rainfall for",monthNames[month], "in inches")
x = input()
months.append(float(x))
ANSWERS = ANSWERS + 1
except:
print ("Try again")
getMonthlyRainfall()
在这种情况下ANSWERS
YEAR_SIZE = 12
months = [] ###This is the array that will hold the rainfall for each month
monthNames=['January','February','March','April','May',
'June','July','August','September',
'October','November','December']
def getMonthlyRainfall():
while True:
total = 0
for month in range(YEAR_SIZE): ###This loop iterates 12 times for 12 entries1
try:
tmp = get_input(month)
except ValueError:
print ("Enter the rainfall for",monthNames[month], "in inches")
tmp = get_input()
months.append(tmp)
continue
def get_input(month):
try:
print ("Enter the rainfall for",monthNames[month], "in inches")
tmp = float(input())
except ValueError:
get_input(month)
这是一个更简洁的答案:
import calendar
def ask_for_rainfall(month_name):
while True:
try:
return float(input("Enter the rainfall for %s in inches" % month_name))
except:
print('Try again')
def get_monthly_rain_fall():
month_names = list(calendar.month_name[1:])
return {m_name: ask_for_rainfall(m_name) for m_name in month_names}
# Now you can do
# rain_falls = get_monthly_rain_fall()
# print(rain_falls["January"])