I'm new to the Python community, so please be gentle and explain concepts and details slowly until I am able to grasp them.
I'm following the code examples in my book, using Python IDLE and Sublime Text 3 as my editors. When, I am ready to test my work using Python IDLE, Python or Sublime Text 3 the following happens:
Either the Python window runs the program quickly and closes out. (Python)
I input a value and it hangs there (Python IDLE and Sublime Text 3)
Yet, when I do:
print("Hello")
I have no issues.
What am I missing or not doing correctly, so when I start testing my scripts, I don't keep running into the same issues listed above.
I have uninstalled Python and Sublime Text 3 and reinstalled still getting the same results.
I didn't create this, this is one of the examples from the book that I am reading that is not producing an output
def main () :
celsius = eval(input ("What is the Celsius temperature? "))
fahrenheit = 9 / 5 * celsius + 32
print ("The temperature is", fahrenheit, "degrees Fahrenheit.")
main ()
I'm expecting to see when I input a value in Celsius a Fahrenheit value to appear.
I just ran your program, I think you need to make sure that the input you are taking is of type int. Also eval() is not needed.
Try this,
celsius = input ("What is the Celsius temperature? ")
fahrenheit = ((9 / 5) * int(celsius)) + 32
print ("The temperature is", fahrenheit, "degrees Fahrenheit.")
Here is a link to try it out (give it 5 seconds to load).
Related
So I'm starting to learn COBOL, tried my first "hello world" program, and got an error that I can't solve.
this is the code:
*hello
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
PROCEDURE DIVISION.
DISPLAY 'HELLO'.
STOP RUN.
I'm using vs code with extensions that talk with gnucobol(openCobol), did that with the help of this video (using windows). When I'm running the code, I get this message:
hello.cbl:1: error: PROGRAM-ID header missing
I've tried to copy the code from a few other sources that have an example code but still got this message.
I would appreciate any help.
It might help you to see what your code looks like in a purpose-made IDE for COBOL. Here is a direct copy & paste of your code in OpenCobolIDE:
The main issue is the position of the code on lines 2 through 5. COBOL generally will require your code to be very specifically formatted, as you can see from the red lines above, there are limits to where data should exist. This can be changed, but I recommend keeping with it as this may increase your comfort in working with the legacy code that runs the world. Here is an example of how this should be written. I will share both a picture and the code itself so you can see where it is in relation to those red lines.
Code with errors still to show as an example:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
*this is a comment
*this is not a comment
MAIN-PROCEDURE.
DISPLAY "Hello world!"
DISPLAY "This display line is too long, creating the same issue but on the other side"
*STOP RUN has an error because the above line was cut off inappropriately
STOP RUN.
END PROGRAM HELLO.
Working code with no errors:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
*this is a comment
MAIN-PROCEDURE.
DISPLAY "Hello world!"
STOP RUN.
END PROGRAM HELLO.
Definitely check out OpenCobolIDE which is a great free IDE for getting started with COBOL and getting the basics down! It helped me to get a better handle on formatting when I was new.
So I downloaded and installed netbeans because my community college used to require it and now I am most comfortable using it rather than other programs.
But whenever I run a program, in the output box, it shows something like
PID=50
TTY=/dev/pt y0
PSID=50
NBMAGIC=49
and then show the output of my program.
And often, it doesn't completely show the output at all.
Like if the output is "Hello World,"
sometimes it just outputs "H" (even when my code is correct)
then after running program again (no change in the code) it shows "Hello World"
Does anyone know how to fix this?
I am trying to solve this HackerRank's problem about dynamic programming. I think I have come with a solution, probably not very efficient but still, I am trying.
I submitted my code and it failed to pass a large test case, so I am trying to test it myself using that test case. The problem is when I enter the input data Xcode doesn't respond, it doesn't crash, but it doesn't continue the execution of the code.
First I had this code to read a single line that contains n space-separated integers, which in this case is 68,738.
let arr = readLine()!.characters.split(" ").map({ Int(String($0))! })
After a few time (several seconds, maybe even minutes) the code crashed saying that it found nil while unwrapping an optional value.
So I tried and split that instruction as follow:
let input = readLine()!
let arr = input.characters.split(" ").map({ Int(String($0))! })
Here I would expect the code to crash in the second line, trying to map the input string to an array of integers. But the code crashed while trying to readLine(). The input string was 370,112 long.
I also tried to use this code in order to at least get the string input:
let input = readLine()
let arr = input!.characters.split(" ").map({ Int(String($0))! })
But input is nil. I assume here that the input string was too long, but shouldn't be 2,147,483,648 on a 32-bytes CPU? I guess that's enough space, right?
I googled to find if there'd be any limit in readLine() but found nothing. I would try to solve this problem in another language but I'd really like to make it in Swift. Is there something I am not seeing?
readLine() is a wrapper around the stdio getline function, and that function requires
only that a newline character occurs within the first SSIZE_MAX characters of the input. On the 64-bit OS X platform, SSIZE_MAX
is 2^63 - 1 which means that this is only a theoretical limitation.
So readLine() is not the problem, it can read arbitrarily long lines as long as they fit into your computers memory.
But it seems that you can not paste more than 1023 characters into the
Xcode debugger console. (Edit: This was also observed at Read a very long console input in C++).
Running the program in a Terminal with input redirection from a file is one option to solve the problem:
$ ./myProgram < /path/to/inputData.txt
Another option is to add
freopen("/path/to/your/inputData.txt", "r", stdin)
at the beginning of the Swift program. This redirects the standard
input to read from the given file.
The advantage of this method is that you can still debug your program
in Xcode.
As mentioned by #MartinR, the problem was I was trying to test this with Xcode, which seems to have some limits for input strings. I tried from the terminal and it worked just the way it should.
I am finding seemingly random ordering of print versus python logging outputs to the pydev console.
So for example here is my code:
import logging
logging.warning('Watch out!') # will print a message to the console
print "foo"
logging.info('I told you so') # will not print anything
logging.warning("Event:")
logging.warning("told ya")
Note the print line around "foo". What is bizzare is each time I run this the order of output with respect to foo changes! That is foo appears one time at the top another time in the middle another time at the end and so forth.
This is unfortunate in a less toy context when I want to know the sequence in which these output events occured in the code (e.g. when tracing/logging etc). Any suggestions as to what might be going on? Latest pydev, python 2.7.6
Thanks!
p.s. As a side-effect (probably) this is making an eclipse tool out there "grep console" behave oddly). But the problem I describe above is independent of that.
I'm learning python online at the moment and using the (macOS) Canopy install of python. The lesson was how to use the quit() function with a try except. I get this error in Canopy:
---> 11 quit()
13 print 'Your number is:', number
NameError: name 'quit' is not defined
----------------- here is the code:
try:
inpt = raw_input('Enter a number: ')
number = float(inpt)
except:
print 'Error, please enter a numeric number'
quit()
print 'Your number is:', number
All the code does is print out your number but if you put something in that's not a number, it says 'Error, please enter a numeric number', instead of throwing an error.
Same code works fine using terminal. Now I'm wondering, should I be using Canopy or am I missing something?
Thanks
Canopy's Python shell is IPython's QtConsole. IPython has taken the scientific Python world by storm in recent years for its power and convenience, and in most respects it is a proper superset of standard Python, but a few of its small convenience changes can be confusing for beginners. quit is one of those small changes. (A more commonly confusing one is described at https://support.enthought.com/entries/25750190-Modules-are-already-available-in-the-pylab-python-prompt-but-not-in-a-script).
For this exercise, I would suggest simply replacing quit() with the equivalent import sys;sys.exit()