different behavior of python result in macro!

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
imanipourmeysam
Posts: 23
Joined: Thu Nov 18, 2021 2:41 pm

different behavior of python result in macro!

Post by imanipourmeysam »

Hi folks.
I am trying to run a very simple code in python console of FreeCAD.
But the problem is that I get unexpected result in the FreeCAD while if I run the same code in python it will be the way it intended to act.

Here is the code:

Code: Select all

def next():
    while(True):
        userinput = input()
        if(userinput == 'y'):
            print("bravo")
            return
        else:
            print("not good answer")
            
The result in python is that if I press 'y' it will print "bravo" and finish the function by return, and otherwise it will print "not good answer" but in FreeCAD it will from the first without waiting for my input print "not good answer" and doesn't matter if I press the 'y' key as the input or other key, the printing will be always "not good answer".

Please let me know if there is something I missed and why is that.
Thank in advance.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: different behavior of python result in macro!

Post by TheMarkster »

Ensure only the y is on the python console. Backspace to clear the line before entering y and enter.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: different behavior of python result in macro!

Post by openBrain »

Running this may help

Code: Select all

print(f"{userinput=}")
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: different behavior of python result in macro!

Post by onekk »

You are using a sort of "low level" code on a console, that is a different thing that a command line python interpreter.

Due to the way it is implemented, probably using input() is not the most "error free way".

As Python console is intended to be a proper "input console", in other word it process single commands, not to write a program with methods and all.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply