Read Print

This is the place for queries that don't fit in any of the other categories.

Read Print

Postby dday9 » Fri Oct 11, 2013 9:00 pm

Hi, please go easy on me I just started learning Python today. I come from a vb.net background and in vb.net we'd write this:
Code: Select all
Console.WriteLine("Hello World")
Console.ReadLine()


To display Hello World and keep it displaying on the command prompt. In Python, I know you call print to display the text, but how would you keep the text on the command prompt. For example:
Code: Select all
print ("Hello World")


If I were to run that, the command prompt would just open and close before I got to see what was printed. Any help is appreciated.

-David
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Read Print

Postby stranac » Fri Oct 11, 2013 9:17 pm

You could do the same thing:
Code: Select all
print("Hello World")
input()

But that's rarely useful. Python should always be executed from the terminal, which will, among other useful things, keep the terminal open.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Read Print

Postby dday9 » Fri Oct 11, 2013 10:02 pm

Well see, right now the only practical program I've made is a Celsius to Fahrenheit converter which looks like this:
Code: Select all
def c_to_f(celsius):
   f = int(celsius) * 9 / 5 + 32
   return f
   
while 1 == 1:
   print ("Please enter in a Celsius value:")
   celsius = int(input())
   
   fahrenheit = c_to_f(celsius)
   print (str(celsius) + " degrees Celsius is " + str(fahrenheit) + " degrees Fahrenheit!")
   
   if celsius == 0:
      print (str(celsius) + " degrees Celsius or " + str(fahrenheit) + " degrees Fahrenheit is water's freezing temperature!")
   elif celsius == 100:
      print (str(celsius) + " degrees Celsius or " + str(fahrenheit) + " degrees Fahrenheit is water's boiling temperature!")
      
   print ("")


So I haven't had to keep the command prompt open for no reason yet, but I wanted to know just in case I may need it in the future.
dday9
 
Posts: 17
Joined: Fri Oct 11, 2013 8:55 pm

Re: Read Print

Postby metulburr » Fri Oct 11, 2013 10:10 pm

This is a usual question regardless of the computer programming language, normally occuring in Windows. You can either A) run the program from the command console/terminal (which will run the program through and return back to the prompt allowing the user to see the output), or B) use some keyboard input to force user input before the console closes, to alow the user to be able to view the output and close the console when they choose.

All of which stranac already described.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1387
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Baldyr, Google [Bot] and 3 guests