Python Forum

Full Version: python console executes a file that PyCharm cannot execute
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ??
What do you mean not correct? Are you expecting the template to change?
With PyCharm the output is line 5. Not what gives python console.
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.
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.
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.
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.