Script running on Linux gives NameError [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I have a python script running on a Linux server. The code:
#!/bin/python
databaseRun = input("Do you want to run all databases: ")
if databaseRun == "yes":
print("yes")
else:
print("no")
This returns and error:
Traceback (most recent call last):
File "./db_upg.py", line 3, in <module> databaseRun = input("Do you want to run all databases: ")
File "string", line 1, in <module> NameError: name 'yes' is not defined
Now if I type this code into PyCharm it runs with no problem. What am I missing? Basically you will put yes or no will be the only to answers

You're probably running this with Python 2 instead of Python 3. Try running it with python3 <scriptname> instead of python <scriptname>. And change the shebang line from #!/bin/python to #!/usr/bin/python3 respectively.
In Python 2, input() reads the input as a code instead of a string. Thus when you type yes, it reads it as a variable yes which is undefined. If you want to run this with Python 2 instead of Python 3 as your tag suggests, use raw_input() instead of input(). Python 2's raw_input() returns a string, similar to Python 3's input().

Related

Why aren't I able to perform simple arithmetic operations in python using Vscode but I can elsewhere [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
essentially when I run something as simple as '3 * 3' in any other IDE I have no issues, however, all arithmetic operations in my Vscode seem to return nothing, the terminal just repeats my PWD essentially.
Note that I am new and learning python but everything else thus far has worked fine, print statements print to the terminal etc but maths docent seem to work.
enter code here: def example(val1, val2):
return val1 * val2
example(2, 6)
Edit: by terminal I mean I am writing the script and clicking the run button in vscode which outputs to said terminal, I have uploaded a SC of what I mean :screenshot of the IDE and terminal output
In some environments, such as the Python REPL (the P stands for "print") and Jupyter Notebook), results are printed out automatically. However, when you run a script from the command line or VS Code, printing isn't automatic. To output something in this case, you need to add a print() command:
def example(val1, val2):
return val1 * val2
result = example(2, 6)
print(result)
Your terminal is running a shell, probably bash not python.
Then, if you want to evaluate that in bash do
$(( 3 * 3 ))

Python - IndexError: list index out of range while running python code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Please I need assistance with this code. I keep getting the error message above on line 5 and 35 of the code shown below. What is wrong?
output = sys.argv[1]
main()
sys.argv returns:
The list of command line arguments passed to a Python script. argv[0]
is the script name
So, if you execute the script like this python script.py then sys.argv[0] returns script.py which is the name of script.
As there is no other argument, hence calling sys.argv[1] will raises as IndexError
Now consider another scenario when you execute the script as python script.py tom:
In this case calling sys.argv[1] returns the first command line argument which is tom.
Hope, this helps clear your understanding.
you need to pass argument by running file name like below
python file_name arg1
then only it will work

NameError: name 'Daniel' is not defined when using input() [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm brand new and am trying to make this incredibly basic code to work. When I run the code and type in my name, I get this error,
Traceback (most recent call last):
File "/Users/dbrown/Desktop/Name", line 2, in <module>
name= input()
File "<string>", line 1, in <module>
NameError: name 'Daniel' is not defined
This is the original code:
print ("Please enter your name.")
name= input()
print ("Hello," , name)
The problem is that this code was written for Python 3, but you're running it with Python 2.
In Python 2 you need to use raw_input instead of input.
The Python 2 version of input is
Equivalent to eval(raw_input(prompt)).
(See https://docs.python.org/2/library/functions.html#input.)
In other words, whatever you enter will be interpreted as Python code. In particular, something like Daniel will be looked up as a variable name. The NameError is thrown because there is no Daniel variable defined in your program.
You are writing code for Python version 2, please consider changing to Python 3.
In python2, there's two console-input functions - input() and raw_input().
input() is for reading integers, and raw_input() is for reading strings. So under Python2, the following works:
print ("Please enter your name.")
name = raw_input()
print ("Hello, " + name)
Under Python 3, your code wortks fine already.
Your code is failing, because Python2 is trying to interpret "Daniel" as a python statement.
Its because python 2, raw_input is python 3's input, and python 2's input is python 3's eval(input(...)), so basically just as here Daniel it tries to access that variable, but it's not defined so error is raised

NameError: name 'ftplip' is not defined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Following is my python code to make a FTP connection
upload_ftp.py
import ftplib
ftp = ftplip.FTP()
ftp.connect('ip', 21)
print ftp.getwelcome()
try:
print "Logging in..."
ftp.login("username", "password")
except:
"failed to login"
but when i run the code ,i get the following error: NameError:name 'ftplib' is not defined
my#my-pc:/var/www$ python upload_ftp.py
Traceback (most recent call last):
File "upload_ftp.py", line 8, in <module>
ftp1 = ftplip.FTP()
NameError: name 'ftplip' is not defined
Any help would be appreciated..
You have a spelling error in your variable name. So Python thinks you are using an undefined variable.
You have a typo: you writing ftplip.FTP() but it is ftplib.FTP() (lip with p vs lib with b).
Use :
from ftplib import FTP
ftp = FTP()
Return a new instance of the FTP class.

Python, got an unexpected keyword argument in my python code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My python code keeps giving me this error
this is the function I try to call with underneath it the code that calls it.
from sys import stdout
def print_nested_list(lijst, indent=False, indent_level=0, fh=stdout):
for x in lijst:
if isinstance(x, list):
print_nested_list(x, indent, indent_level+1, fh)
else:
if indent:
for tabstop in range(indent_level):
print("\t", end='', file=fh)
print(x, file=fh)
try:
with open(r'C:\Python34\headfirstpython\chapter 3\man_data.txt', 'w') as man_data:
print_nested_list(man, fh=man_data)
with open(r'C:\Python34\headfirstpython\chapter 3\other_data.txt', 'w') as other_data:
print_nested_list(other, fh=other_data)
IDLE gives this error when i try to run it
Traceback (most recent call last):
File "C:\Python34\headfirstpython\chapter 3\sketch1.py", line 25, in <module>
print_nested_list(man, fh=man_data)
TypeError: print_nested_list() got an unexpected keyword argument 'fh'
I don't understand what is wrong with my function or the way I call my function?
In the argument list, you don't have 'fh' - you have 'fh_id'. Try using 'fh' instead.
Your function doesn't have a fh keyword argument. It has a fh_id keyword argument though.
Either fix your function signature (rename fh_id to fh) or your call (use fh_id instead of fh).

Categories

Resources