how to display the print message from the function imported from another file when called from inside an if block
Question:
I create a python function in a file and filename is ifunc.py
def func():
print('print something')
I have my main program in another file like this:
import ifunc
inputFlag = input('input something:')
if inputFlag == 1:
ifunc.func()
and this produce nothing, but when I try to import the function outside the if statement like:
import ifunc
ifunc.func()
the print message print something
will show up.
So how could I make the print message show up when the called function inside the if statement?
Please give me some advice.
Thanks in advance!
Answers:
You should change the below statement in main code:
The input you are taking is as a string
. And if
statement checks value of inputFlag
to be 1
which is int
and not string
.
inputFlag = int(input('input something:'))
I create a python function in a file and filename is ifunc.py
def func():
print('print something')
I have my main program in another file like this:
import ifunc
inputFlag = input('input something:')
if inputFlag == 1:
ifunc.func()
and this produce nothing, but when I try to import the function outside the if statement like:
import ifunc
ifunc.func()
the print message print something
will show up.
So how could I make the print message show up when the called function inside the if statement?
Please give me some advice.
Thanks in advance!
You should change the below statement in main code:
The input you are taking is as a string
. And if
statement checks value of inputFlag
to be 1
which is int
and not string
.
inputFlag = int(input('input something:'))