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
  Locally run an APK and execute functions using Python KovyJ 0 425 Jan-23-2025, 05:21 PM
Last Post: KovyJ
  Pycharm can't read file Genericgamemaker 5 1,528 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  updating pip, python - understanding terminal and pycharm. endlessnameless 0 1,094 Jun-24-2024, 05:48 AM
Last Post: endlessnameless
  In Console,Python anna17 0 679 Mar-23-2024, 08:24 PM
Last Post: anna17
  Failed to execute child process (No such file or directory) uriel 1 2,358 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Can't update new python version on Pycharm GOKUUUU 6 7,652 Jul-23-2022, 09:24 PM
Last Post: GOKUUUU
  How To Set Up And Execute A Python File knight2000 4 3,938 May-31-2022, 10:02 AM
Last Post: Larz60+
  Python Flask Realtime system printout (console) ffmpeg jttolleson 3 4,345 Apr-18-2022, 06:39 PM
Last Post: jttolleson
  console pwd file kucingkembar 4 2,515 Jan-09-2022, 08:31 PM
Last Post: kucingkembar
  execute python script guy7200 1 2,137 Oct-25-2021, 09:55 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

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