If I want to print the results of a python script or an other program to a file, I simple do something like this on the console python myscript.py>output.txt, but now myscript has an input inside and how can I get this to the console? For example I have script to check if a given year is a leap year or not:
def isLeapYear(year):
return (year%4==0 and (year%400==0 or year%100!=0))
if __name__ == "__main__":
y=input("Test if your year is a leap year: ")
try:
out=str(y)+" is"
if not isLeapYear(int(y)):
out+=" not";
print(out+" a leap year")
except ValueError:
print("invaild input")
When I now write the command above, the result in my output-file is something like this (while I can just enter the year in the console):
Test if your year is a leap year: 2000 is a leap year
but I just want
2000 is a leap year
in there and have
Test if your year is a leap year:
shown on the console. Is there a clever way to do this? Can I somehow force print something to the console? Is there a general way to this? I mean I could have the same problem with a C or Java-Program.