错误捕获与计数器不工作相结合
Error trapping combined with a counter not working
我正在制作一个代码,让用户逐行输入 RLE。然后我将输入的数据发送到对其进行解码的函数。在该函数中,我包含了一些基本的错误捕获。
我的错误是,当用户只能输入错误数据时,他们选择的次数(他们选择他们想要输入的行数)。也就是说,如果用户输入了 2 行(共 3 行)正确的 RLE 行,然后输入了错误的行,代码将不会要求再次输入 RLE,但如果他们在第一次或第二次输入时输入了错误的行,它就会起作用。
代码:
if line_amount>2:
print ("Please enter the compressed data one line at a time")
while line_amount > counter:
compressed_data = input('->') #ENTER RLE DATA
counter+=1
RLEtoASCII(compressed_data,counter)
RLE转ASCII:
def RLEtoASCII(compressed_data,counter):
try:
pairs = [(int(compressed_data[i:i+2]), compressed_data[i+2]) for i in range(0, len(compressed_data), 3)]
global text
text = ''.join(n * c for n, c in pairs)
RLE_Inputs = open("Entered Data.txt", 'a+') #File that lists all the inputs
#[etc all the file writing]
except:
print('THERE WAS A PROBLEM WITH THE VALUES Please re-enter values.\n')
如果我尝试在 except 之后调用 RLEtoASCII
,它会创建一个循环。 counter -=1
之后似乎不起作用,除了...
尝试将 while line_amount > counter:
中的 >
更改为 >=
。这是因为当您到达第三行时,它正在测试 if 3>3 是否为假,而 3>=3 是否为真。
问题似乎是即使您发现错误,counter
也会递增。我将 counter 更改为全局,以便在出现错误时它不会有效地增加。只需确保将 global counter
置于定义之上即可。让我知道这是否适合你。
if line_amount>2:
print ("Please enter the compressed data one line at a time")
while line_amount > counter:
compressed_data = input('->') #ENTER RLE DATA
counter+=1
RLEtoASCII(compressed_data)
RLE转ASCII:
def RLEtoASCII(compressed_data):
global counter
try:
pairs = [(int(compressed_data[i:i+2]), compressed_data[i+2]) for i in range(0, len(compressed_data), 3)]
global text
text = ''.join(n * c for n, c in pairs)
RLE_Inputs = open("Entered Data.txt", 'a+') #File that lists all the inputs
#[etc all the file writing]
except:
print('THERE WAS A PROBLEM WITH THE VALUES Please re-enter values.\n')
counter-=1
通过将错误处理移出您的函数并移至 if 循环中,我们可以更轻松地控制迭代:
if line_amount>2:
print ("Please enter the compressed data one line at a time")
while line_amount > counter:
compressed_data = input('->') #ENTER RLE DATA
if compressed_data != '':
try:
RLEtoASCII(compressed_data)
counter+=1
except:
print('THERE WAS A PROBLEM WITH THE VALUES Please re-enter values.\n')
RLE转ASCII:
def RLEtoASCII(compressed_data):
pairs = [(int(compressed_data[i:i+2]), compressed_data[i+2]) for i in range(0, len(compressed_data), 3)]
global text
text = ''.join(n * c for n, c in pairs)
RLE_Inputs = open("Entered Data.txt", 'a+') #File that lists all the inputs
#[etc all the file writing]
我正在制作一个代码,让用户逐行输入 RLE。然后我将输入的数据发送到对其进行解码的函数。在该函数中,我包含了一些基本的错误捕获。
我的错误是,当用户只能输入错误数据时,他们选择的次数(他们选择他们想要输入的行数)。也就是说,如果用户输入了 2 行(共 3 行)正确的 RLE 行,然后输入了错误的行,代码将不会要求再次输入 RLE,但如果他们在第一次或第二次输入时输入了错误的行,它就会起作用。
代码:
if line_amount>2:
print ("Please enter the compressed data one line at a time")
while line_amount > counter:
compressed_data = input('->') #ENTER RLE DATA
counter+=1
RLEtoASCII(compressed_data,counter)
RLE转ASCII:
def RLEtoASCII(compressed_data,counter):
try:
pairs = [(int(compressed_data[i:i+2]), compressed_data[i+2]) for i in range(0, len(compressed_data), 3)]
global text
text = ''.join(n * c for n, c in pairs)
RLE_Inputs = open("Entered Data.txt", 'a+') #File that lists all the inputs
#[etc all the file writing]
except:
print('THERE WAS A PROBLEM WITH THE VALUES Please re-enter values.\n')
如果我尝试在 except 之后调用 RLEtoASCII
,它会创建一个循环。 counter -=1
之后似乎不起作用,除了...
尝试将 while line_amount > counter:
中的 >
更改为 >=
。这是因为当您到达第三行时,它正在测试 if 3>3 是否为假,而 3>=3 是否为真。
问题似乎是即使您发现错误,counter
也会递增。我将 counter 更改为全局,以便在出现错误时它不会有效地增加。只需确保将 global counter
置于定义之上即可。让我知道这是否适合你。
if line_amount>2:
print ("Please enter the compressed data one line at a time")
while line_amount > counter:
compressed_data = input('->') #ENTER RLE DATA
counter+=1
RLEtoASCII(compressed_data)
RLE转ASCII:
def RLEtoASCII(compressed_data):
global counter
try:
pairs = [(int(compressed_data[i:i+2]), compressed_data[i+2]) for i in range(0, len(compressed_data), 3)]
global text
text = ''.join(n * c for n, c in pairs)
RLE_Inputs = open("Entered Data.txt", 'a+') #File that lists all the inputs
#[etc all the file writing]
except:
print('THERE WAS A PROBLEM WITH THE VALUES Please re-enter values.\n')
counter-=1
通过将错误处理移出您的函数并移至 if 循环中,我们可以更轻松地控制迭代:
if line_amount>2:
print ("Please enter the compressed data one line at a time")
while line_amount > counter:
compressed_data = input('->') #ENTER RLE DATA
if compressed_data != '':
try:
RLEtoASCII(compressed_data)
counter+=1
except:
print('THERE WAS A PROBLEM WITH THE VALUES Please re-enter values.\n')
RLE转ASCII:
def RLEtoASCII(compressed_data):
pairs = [(int(compressed_data[i:i+2]), compressed_data[i+2]) for i in range(0, len(compressed_data), 3)]
global text
text = ''.join(n * c for n, c in pairs)
RLE_Inputs = open("Entered Data.txt", 'a+') #File that lists all the inputs
#[etc all the file writing]