I haven't really ran into many issues with python but I'm working on a side project and the input() function wasn't working. I created a new file to test out a simple input() function and the same thing happened, the program doesn't take any input and it instead seems to take in the pyenv version or something? Anyone know how to fix this?
# Taking input from the user
name = input("Enter your name")
# Print input
print("Hello", name)
Pasted output from terminal:
Enter your namepyenv shell 2.7.18
Hello pyenv shell 2.7.18
Related
I'm wondering how I can utilize my code on IDLE to work within the macOS Terminal.
For example, I created a function such as:
def multiplication_by_2(x): return 2 * x
and saved the .py file in a desktop folder.
I want to use terminal to test out various cases such as multipication_by_2(100) etc, however I am unsure about which commands to enter in terminal to achieve this.
Any direction toward this would be helpful. Thank you.
Try this at the end of the code:
number = int(str(input("Enter the number you would like to multiply by 2: ")))
multiplication_by_2(number)
This way, you get user input. Then, in the terminal:
$ python3 <filename>.py
Which should produce the output:
Enter the number you would like to multiply: <your input, ex. 100>
200
Hope that solved it!
If you are asking how to send arguments to your program through the command line, python's sys library has a list named argv that holds arguments passed from the command line. Add this to your python file:
from sys import argv
for argument in argv:
print(multipication_by_2(int(argument))) # All arguments are strings by default
Then in the command line, do python file_name.py 20 50 1 and any other number you might want to try, and the program will print its double.
Note: If your command line says python doesn't exist, try python3.
The code in the interactive shell:
>>>password = ''
>>>while password != 'secret':
password = input('Please password')
print("Right password")
SyntaxError: invalid syntax
>>>
I want to make a code where he will ask me to enter the code and i can do it ... But when i press enter I get the following message.
python3
https://imgur.com/a/7z6CaTu
Please note input() and print() in Python2.x is a little different with python3.x
Therefore, Check your python version first.
Python2.x:
password = raw_input('prompt to user')
print 'hello world'
Python3.x:
password = input('prompt to user')
print('hello world')
Then, when you define a while loop in the command line, you should leave a line empty and press Enter after while loop completed, enter the rest of your code.
As I showed in the above picture, I left a line empty then press enter, then I entered my rest of codes.
If you wanna run your code in Python v3, in the command prompt or Linux shell enter: python3
If you got error please download the latest version of python
Finally, as others say, please use an IDE like vscode and enter your entire code then run it, less trouble!!!
Download VisualStudio Code:
(https://code.visualstudio.com/download).
Python shell doesn't support the complex code. Maybe You can try this.
Python shell with while
Anyway, I recommend you to use the script.
I've tried to run multiple python functions on mac terminal and they all return syntax error, for this program
def spam():
print "R"
spam()
it returned the error:
./test.py: line 1: syntax error near unexpected token `('
./test.py: line 1: `def spam():'
this is really the simplest function I could find.
Just to be clear terminal is running the rest of the program but it can't handle functions.
#!/usr/bin/python
import math
number = int(raw_input("What's your surd?"))
print type(number)
#Just to let us know what the input is
if type(number) == int:
print "Number is an integer"
else:
print "Please enter a number"
value = math.sqrt(number)
#Takes the number and square roots it
new_value = int(value)
#Turns square root of number into an integer
if type(new_value) == int:
print "Surd can be simplified"
print new_value
else:
print "Surd cannot be simplified"
print value
This program runs fine even if it is a bit buggy at the moment but the following program returns the same error as the previous function.
# define a function
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = int(input("What's your number? "))
print_factors(num)
Why terminal is returning a syntax error where there isn't one?
The problem here (at least for that first example) is that you're not using a python interpreter. The terminal is using a bash interpreter for your python code and getting very confused. Use a command like this to execute your code python spam.py . Or first enter the python command interpreter by running python, then entering your code in the command line interpreter.
What might be even easier while getting started is to get an IDE like PyCharm (https://www.jetbrains.com/pycharm/) and running through a couple of their tutorials to get the feel of it.
Your issue is that your shell doesn't know you are running a Python script. You need to make it clear that you should be using the Python interpreter. You can either do this by:
1) Call python test.py at your terminal.
2) Add #!/usr/bin/python at the top of your Python script (you may need to alter the path to the Python executable on your system). Make the script executable, and call ./test.py at your terminal.
The benefits of 2) are that you know what version of Python you will be running your script with (Python 2.x in your case?).
Method 1) will use whatever Python version is encountered first in your PATH, which may be Python 3 or Python 2, depending on whether you have installed Python 3 at some point. The code you have written will work with Python 2.7, but not Python 3.x. Of course you can always explicitly call python2.7 ./test.py.
Noob question.
I'm trying out Portable Python, but I can't get it to load a script from the Python-Portable.exe executable. I'm running from command line:
> Python-Portable.exe hello.py
To get it to load hello.py, which I put at the same level as the exectuable and just in case, in the same level as the real python.exe executable. It starts Portable Python, shows a splash screen for a second, then shows a console window and immediately closes it.
However, if I use command line to start python.exe directly, passing it hello.py it works correctly. Two questions then -
Why doesn't it work using Python-Portable.exe?
What'd the difference between starting Python-Portable.exe and starting Python.exe directly?
EDIT: Here is hello.py, its the example used on the Portable Python website.
print("Hello world")
a = True
if a == True:
print("It is true!")
else:
print("It is false...")
b = raw_input("Enter value:")
print("Your value is")
print(b)
raw_input("Press enter to continue...")
Typo in hello.py.
Bad code is not run, and you don't get any feedback.
This question already has answers here:
error in python d not defined. [duplicate]
(3 answers)
Closed 8 years ago.
I have a super basic question, but I'm just starting to learn python. My script:
print('What is your name?')
person = input("Enter name: ")
print("Hello ", person)
is returning an error: NameError: name 'Bob' is not defined.
I have basically just copied and pasted what was from the tutorial at this point, but it still doesn't work unless I put the name in quotation marks. What am I doing wrong?
Your code should work perfectly fine in Python 3. However, in Py2 it will throw a NameError as there are differences between input() and raw_input(). Essentially, input() in Python 2 is the same as eval(raw_input(""Enter name: ")), meaning that it will attempt to run the inputed code as Python.
In Python 3, raw_input() is no more, and input() operates the way you are expecting it to here: Print a line, accept input, and assign it in string format to a variable.
You simply are not using Python 3. In Python 2, input() works differently; see this excerpt from the docs.
Equivalent to eval(raw_input(prompt)).
So, when you type Bob without quotation marks, you are basically saying eval(Bob). With quotes it is eval("Bob"). raw_input() does not exist in Python 3, so it will not be defined if you are running with Python 3.
Make sure that you are running your file with Python 3:
Type which python and which python3 to make sure you have both.
Run python and python3 and see which versions you are running when the python shell opens.
You can check the version of Python which is running your code by doing import sys; print(sys.version).
Run your code via python3 yourfile.py.
As J.F. Sebastian notes, you should add #!/usr/bin/env python3 to the very first line of your file. Chances are, you either wrote #! /usr/bin/env python which on OS X is 2.7.x (I think it's 2.7.2, and I think python is a symlink to the python27 binary) and are running your file just by typing its name, or you are running it through python.