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()
Related
I installed an Agda compiler, binarys can be from here: http://ocvs.cfv.jp/Agda/how-to-install-windows.html
... and I'm trying to make it compile a simple hello world app (I found the Agda 'Hello World' code online)
But I've never used Emacs before, and I don't know where to begin, or which commands to use to compile and run. I'm new to Agda, which seems to have limited options for compilers, and is lacking any step by step tutorial. Below is a screenshot of the Emacs compiler with the code I found:
open import System.IO using ( _>>_ ; putStr ; commit )
module System.IO.Examples.HelloWorld where
main = putStr "Hello, World\n" >> commit
I'm looking for step by step instructions to run a simple 'Hello World' program
A working example with another compiler would also be an acceptable answer
Thanks!
This looks like you attempted something like the general M-x compile rather than any specific Agda functionality.
The Agda:run mode indicator in the Emacs mode line suggests that you have a running Agda process in another buffer, but you're not looking at it. The Agda mode probably has something like agda-eval-buffer which should pass your current program to that process, and bring up the results in the lower half of the pane. (Try switching to a buffer called something like *inferior-agda* manually if you somehow cannot reach that buffer by other means.)
The site you link to says it's Agda 1 and you should probably actually look for Agda 2 on a different site.
Below the line is my original answer, which may still provide some useful insight.
The error message indicates that you need to install make.
I'm guessing there may be additional missing dependencies after you fix this one. Ideally the documentation should explicitly specify exactly what you need to install.
make is just a wrapper to run whatever cormands are found in the local Makefile. If there is no such file, you will probably want to change the compilation command to something else. (Typically Emacs asks you for a command to run, but supplies a plausible default.)
Given I am running on Linux and am no agda expert, this solution might not be worth it. But still I will give it a try.
When I installed agda and agda-stdlib on my system, it provides me with a file called agda2.el in /usr/share/agda/emacs-mode. That said I just had the following in my ~/.emacs.d/init.el file:
(load-file (let ((coding-system-for-read 'utf-8))
(shell-command-to-string "agda-mode locate")))
Since, you already have agada mode setup in Emacs the above wont be useful unless your version of agda mode is old.
We can compile the current file you have opened in Emacs using M-x agda2-compile. Doing this will open up another prompt asking you for a Backend. I used GHC as input and it compiled it. Yes, and I got some errors I don't know how to fix. So, I queried on a search engine and came up with:
module memo where
open import IO.Primitive using (IO; putStrLn)
open import Data.String using (toCostring; String)
open import Foreign.Haskell using (Unit)
main : IO Unit
main = putStrLn (toCostring "Hello, Agda!")
I need to point out that the first line module memo where should be same as the filename which for your case is memo.agda.
I now have a hello world program running on my machiene.
The following code compiles and works
open import Common.IO
main = putStrLn "Hello, world, strings working!"
is the code, stored in the file 'hello.agda', which I compile in emacs to 'hello'. I compile in emacs by selecting agda > compile, an option that is available on emacs when agda is installed correctly.
I can't give a detailed tutorial as to how to install agda on emacs as a friend did it for me, but the above code works, and compiles on emacs on linux, which is the set up which is working for me.
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 have a problem. I want to execute some commands in the Commandline of linux. I tested TProcess (So i am using Lazarus) but now when i am starting the programm, there is nothing, wich the Program do.
Here is my Code:
uses [...], unix, process;
[...]
var LE_Path: TLabeledEdit;
[...]
Pro1:=TProcess.Create(nil);
Pro1.CommandLine:=(('sudo open'+LE_Path.Text));
Pro1.Options := Pro1.Options; //Here i used Options before
Pro1.Execute;
With this Program, i want to open Files with sudo (The Programm is running on the User Interface)
->Sorry for my Bad English; Sorry for fails in the Question: I am using StackOverflow the first time.
I guess the solution was a missing space char?
Change
Pro1.CommandLine:=(('sudo open'+LE_Path.Text));
to
Pro1.CommandLine:=(('sudo open '+LE_Path.Text));
# ----------------------------^--- added this space char.
But if you're a beginner programmer, my other comments are still worth considering:
trying to use sudo in your first bit of code may be adding a whole extra set of problems. SO... Get something easier to work first, maybe
/bin/ls -l /path/to/some/dir/that/has/only/a/few/files.
find out how to print a statement that will be executed. This is the most basic form of debugging and any language should support that.
Your english communicated your problem well enough, and by including sample code and reasonable (not perfect) problem description "we" were able to help you. In general, a good question contains the fewest number of steps to re-create the problem. OR, if you're trying to manipulate data,
a. small sample input,
b. sample output from that same input
c. your "best" code you have tried
d. your current output
e. your thoughts about why it is not working
AND comments to indicate generally other things you have tried.
here are the relevant code lines for a unicode/pyside related problem :
#!/usr/bin/env python
# coding=utf-8
...
msgBox = QtGui.QMessageBox()
msgBox.setText('é')
print 'é'
....
The print does what it should, implying my locale is utf-8, and printenv confirms it. On the other hand, the msgBox shows 'é', unless I prefix the string with 'u'. Is that normal, and do I really have to prefix every string with u in order to use Pyside, when python never raises a problem?
Thanks for your attention.
In Python 2.X there are two data types for strings: byte code and unicode. With u you choose unicode with nothing you choose byte code. If you print bytes('é') you see where PySide/PyQt get their data from. So it probably is normal.
The easy way out is using Python 3.X which just has unicode and won't bother you with the difference.
Not sure who is actually doing something that shouldn't be done, the print function Python 2.X or PySide/PyQt for Python 2.X, so I make it community wiki.
If I run 'python' on the linux command line, and I don't provide any command line arguments, the program displays a welcome message and waits for user input. I would assume that under the hood, the program sends the message to the 'standard output' stream and performs a blocking read on the 'standard input' stream.
If, however, I invoke python indirectly by piping the output from another process (e.g. echo "hello" | python), python does NOT output the message. Somehow, it can tell the difference between the original scenario (a) where the 'standard input' stream is being populated by some other process and (b) where the 'standard input' stream is populated from the keyboard.
How is this accomplished? I had always thought that this information is not available to an application. How would a Java application be able to achieve this differentiation?
Python:
import os
import sys
if os.isatty(sys.stdin.fileno()):
print "is a tty"
else:
print "not a tty"
I'm not aware of a Java equivalent using the default libraries; Jython appears to use native code (jna-posix).
Update - here's two possibilities:
System.console() returns a console object if there is one; null otherwise (see here and here)
System.out.available() returns 0 if there's no input immediately available, which tends to mean stdin is a terminal (but not always - this is a less accurate test)