Python Forum
python console executes a file that PyCharm cannot execute - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python console executes a file that PyCharm cannot execute (/thread-3638.html)



python console executes a file that PyCharm cannot execute - sylas - Jun-09-2017

Hi all. Here at first the file that works well with python console........................................................................................................GCC 5.4.0 20160609] on linux
num1=1.5;num2=6.3
sum=num1+num2
template='{0}, plus {1} gives us {2}'
template.format(num1,num2,sum)
'1.5, plus 6.3 gives us 7.8'


# file2.py

num1=1.5;num2=6.3
sum=num1+num2
template='{0}, plus {1} gives us {2}'
template.format(num1,num2,sum)
print(template)#not correct, but what else ??



RE: python console executes a file that PyCharm cannot execute - micseydel - Jun-09-2017

What do you mean not correct? Are you expecting the template to change?


RE: python console executes a file that PyCharm cannot execute - sylas - Jun-09-2017

With PyCharm the output is line 5. Not what gives python console.


RE: python console executes a file that PyCharm cannot execute - micseydel - Jun-09-2017

I think you're confusing running a script vs. an interactive session. In an interactive session, the result of an expression will be echoed without you having to explicitly print it. Calling format() on a string does not print anything, and does not mutate the string.


RE: python console executes a file that PyCharm cannot execute - buran - Jun-09-2017

line #6 would have visual effect only in console. if you run py file, you would not see it doing anything. you need to print it or assign it to variable and then print that variable

print(template.format(num1,num2,sum))
or

result = template.format(num1,num2,sum)
print(result)
also, use different name, not sum. It's a build-in function and this way you overwrite it.


RE: python console executes a file that PyCharm cannot execute - sylas - Jun-09-2017

Thank you Buran. I used your first suggestion. It works well. I think there is too much code for a simple sum. That comes from Lutz's book.


RE: python console executes a file that PyCharm cannot execute - Skaperen - Jun-10-2017

too much code?  how small do you think the code should be?  mysum=sum(num1,num2)?

some people make their code too small. some people make their code too large.