Forum > Beginners

"Else" behaviour between Python and Pascal [SOLVED]

(1/4) > >>

pascal111:
I programmed this small program of finding the sum of two numbers in both Python and Pascal. The problem faced me is that the "else" keyword doesn't behave the same in both languages "try" blocks what pushed me to make what I want to do to "break" the loop in Pascal code with another way other than the one of the Python model. Is this true that "else" behave the different in both languages?


--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#!/usr/bin/python3  while True:     x=input("Enter a value: ")     try:        float(x)    except ValueError:        print("You entred invalid numeric value!")    else:        break    while True:     y=input("Enter another value: ")     try:        float(y)    except ValueError:        print("You entred invalid numeric value!")    else:        break   print ("The sum of the two values is:",(float(x)+float(y)))  


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program project1; {$mode objfpc}{$H+} uses   {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes, Dialogs, sysutils, forms, LCLType, interfaces  { you can add units after this };   var   s:string;  x, y:real;  ok_value:(indeedOk, notOk); begin   repeat     s:=inputbox('X value', 'Enter x value:','');     ok_value:=indeedOk;     try        finally          try            x:=strtofloat(s);          except           on exception do           begin           showmessage('You entred invalid numeric value!');           ok_value:=notOk;           end;        end;      end;   until ok_value=indeedOk;   repeat     s:=inputbox('Y value', 'Enter y value:','');     ok_value:=indeedOk;     try        finally          try            y:=strtofloat(s);          except           on exception do           begin           showmessage('You entred invalid numeric value!');           ok_value:=notOk;           end;        end;      end;   until ok_value=indeedOk;   showmessage('x + y = '+ floattostr(x+y)); end.  

Bart:
Pascal has try..except and try..finally, no "else" for either of those contructs.

Bart

pascal111:

--- Quote from: Bart on September 10, 2021, 03:16:35 pm ---Pascal has try..except and try..finally, no "else" for either of those contructs.

Bart

--- End quote ---

Return to this formal document: https://www.freepascal.org/docs-html/current/ref/refse119.html#x245-26900017.2

MarkMLl:
Note that try..finally..end and try..except..end are two very different things. Frankly, I'd be happier if it were start..finally..end, or at least if that were allowed as an alternative.

The page you cited shows that you can put an else inside the exception handler which is comparable with a case statement, but not in a finally block. You could probably get away with a break in your exception handler. Try immediately followed by finally is pretty pointless.

MarkMLl

Bart:

--- Quote from: pascal111 on September 10, 2021, 03:23:14 pm ---Return to this formal document: https://www.freepascal.org/docs-html/current/ref/refse119.html#x245-26900017.2

--- End quote ---

Never knew that.
From what I gather it should be to differentiate between types of exceptions, the else clause would then be invoked if none of the types match (this is for the on E: ESomeException do .. variant).

Not sure about the exact syntax.

Bart

Navigation

[0] Message Index

[#] Next page

Go to full version