Python Forum
python console executes a file that PyCharm cannot execute
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python console executes a file that PyCharm cannot execute
#1
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 ??
Reply
#2
What do you mean not correct? Are you expecting the template to change?
Reply
#3
With PyCharm the output is line 5. Not what gives python console.
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply
#7
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  In Console,Python anna17 0 134 Mar-23-2024, 08:24 PM
Last Post: anna17
  Failed to execute child process (No such file or directory) uriel 1 1,616 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Can't update new python version on Pycharm GOKUUUU 6 3,711 Jul-23-2022, 09:24 PM
Last Post: GOKUUUU
  How To Set Up And Execute A Python File knight2000 4 1,825 May-31-2022, 10:02 AM
Last Post: Larz60+
  Python Flask Realtime system printout (console) ffmpeg jttolleson 3 2,861 Apr-18-2022, 06:39 PM
Last Post: jttolleson
  console pwd file kucingkembar 4 1,525 Jan-09-2022, 08:31 PM
Last Post: kucingkembar
  execute python script guy7200 1 1,574 Oct-25-2021, 09:55 PM
Last Post: Axel_Erfurt
  How to use GPU in Python or Pycharm? plumberpy 2 12,915 Oct-11-2021, 12:57 PM
Last Post: plumberpy
  Cannot install tensorflow, numpy etc in Pycharm with Python 3.10 plumberpy 2 3,387 Oct-07-2021, 05:33 AM
Last Post: plumberpy
  PyCharm One Interpreter for every file? muzikman 3 1,828 Sep-28-2021, 02:09 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020