NameError: name 'form' is not defined (Python3) - python

Basically the main method takes user input, checks it and calls the first method if the user doesn't enter quit.
The first method checks the first section of the input and calls one of the other methods depending on what the user enters. This is the point I get an error; when the first method calls the form method, for example, I get an NameError: name 'form' is not defined exception. I'm a little confused about this since I've defined each method and they're all spelt correctly, also when I call the quit method it works perfectly fine.
Main method:
if __name__ == '__main__':
for line in sys.stdin:
s = line.strip()
if not s: break
if (str(s) == "quit"): quit()
elif (str(s) == "quit") == False:
a = s.split()
print(a)
if (len(a) is 2): first(a)
elif (len(a) is 3): first(a)
else: print("Invalid Input. Please Re-enter.")
First method:
def first(a = list()):
word = a[0]
if word == "ls":
ls(a[1])
elif word == "format":
form(a[1])
elif word == "reconnect":
reconnect(a[1])
elif word == "mkfile":
mkfile(a[1])
elif word == "mkdir":
mkdir(a[1])
elif word == "append":
append(a[1], a[2])
elif word == "delfile":
delfile(a[1])
elif word == "deldir":
deldir(a[1])
else:
print("Invalid Prompt. Please Re-enter.")
Other methods (these are all called from the first method):
def reconnect(one = ""):
print("Reconnect")
def ls(one = ""):
print("list")
def mkfile(one = ""):
print("make file")
def mkdir(one = ""):
print("make drive")
def append(one = "", two = ""):
print("append")
def form(one = ""):
print("format " + one)
def delfile(one = ""):
print("delete file")
def deldir(one = ""):
print("delete directory")
def quit():
print("quit")
sys.exit(0)

this error is because of
elif word == "format":
form(a[1])
python basically doesn't know what form is.
let me show you:
gaf#$[09:21:56]~> python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> form()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'form' is not defined
>>>
there is two ways out
>>> def form():
... pass
...
>>> form()
>>> form
<function form at 0x7f49d7f38a28>
>>>
or import it form some library using
import
command
also order is matters too
try:
form()
except NameError:
print('Oops name error raise above')
def form():
print('form foo is called')
try:
form()
except NameError:
print('Oops name error raise below')
will give you
/home/gaf/dashboard/bin/python /home/gaf/PycharmProjects/dashboard/test.py
Oops name error raise above
form foo is called
Process finished with exit code 0
P.S.
take a look at pep8
your code is a mess %)
but no worries this what everybody does with first language

It depends if you use python 2.7 or 3, but your code works with some minor changes.
import sys
def reconnect(one=""):
print("Reconnect")
def ls(one=""):
print("list")
def mkfile(one=""):
print("make file")
def mkdir(one=""):
print("make drive")
def append(one="", two=""):
print("append")
def form(one=""):
print("format " + one)
def delfile(one=""):
print("delete file")
def deldir(one=""):
print("delete directory")
def quit():
print("quit")
sys.exit(0)
def first(a=list()):
word = a[0]
if word == "ls":
ls(a[1])
elif word == "format":
form(a[1])
elif word == "reconnect":
reconnect(a[1])
elif word == "mkfile":
mkfile(a[1])
elif word == "mkdir":
mkdir(a[1])
elif word == "append":
append(a[1], a[2])
elif word == "delfile":
delfile(a[1])
elif word == "deldir":
deldir(a[1])
else:
print("Invalid Prompt. Please Re-enter.")
line = raw_input("Some input please: ") # or `input("Some...` in python 3
print(line)
s = line.strip()
if (str(s) == "quit"):
quit()
elif (str(s) == "quit") == False:
a = s.split()
print(a)
if (len(a) is 2):
first(a)
elif (len(a) is 3):
first(a)
else:
print("Invalid Input. Please Re-enter.")
Test
python pyprog.py
Some input please: ls text.txt
ls text.txt
['ls', 'text.txt']
list
You can also try it online.

Related

why am i getting NameError: global name 'spacing' is not defined

I'm getting a NameError: global name 'spacing' is not defined for and im not sure why
def main():
print("Project One\n")
print("To encrypt message enter: 1")
print("To decrypt messsge enter: 2\n")
pick = int(input())
if pick == 1:
print("Encrypting!")
spacing()
elif pick == 2:
print("Decrypting!")
else:
print("Try again")
if __name__ == '__main__':
main()
def spacing():
text = raw_input("Please Enter the message to encrypt: ").upper()
print(text)
key = raw_input("Please enter a key to encrypt").upper()
print(key)
space_ignore = ""
j = 0
for i in range(len(text)):
if ord(text[i]) == 32:
space_ignore += " "
else:
if j < len(key):
space_ignore += key[j]
j += 1
else:
j = 0
space_ignore += key[j]
j += 1
print(space_ignore)
return text, space_ignore
Python defines are evaluated in-order relative to other statements. By the time main() runs, spacing() has not yet been defined since control has not yet reached def spacing(): .... Move your if __name__ == 'main': block to below the definition of spacing.
Put spacing() definition above main definition and the last thing you need to place is this:
if __name__ == '__main__':
main()
python is a scripting language,would do “line by line” or easy say:from top to end.
make the spacing before main.

"list index out of range" exception (Python3)

I keep getting a list index out of range exception when I check the length of the list a. The error pops up for either the if or elif part of the second if statement, depending on what the user inputs. I know that when the user input is split the list is created correctly because I print it out... So I'm a little lost about why I'm getting that error.
if __name__ == '__main__':
for line in sys.stdin:
s = line.strip()
if not s: break
if (str(s) is "quit") == True: quit()
elif (str(s) is "quit") == False:
a = s.split()
print(a)
if (len(a) == 2) == True: first(a)
elif (len(a) == 3) == True: first(a)
else: print("Invalid Input. Please Re-enter.")
The first method is: (The methods it calls in the if statement just print things out at the moment)
def first(self, a = list()):
word = a[0]
if word is ls:
ls(a[1])
elif word is format:
form(a[1]) # EDIT: was format
elif word is reconnect:
reconnect(a[1])
elif word is mkfile:
mkfile(a[1])
elif word is mkdir:
mkdir(a[1])
elif word is append:
append(a[1], a[2])
elif word is delfile:
delfile(a[1])
elif word is deldir:
deldir(a[1])
else:
print("Invalid Prompt. Please Re-enter.")
Other methods:
def reconnect(one = ""):
print("Reconnect")
def ls(one = ""):
print("list")
def mkfile(one = ""):
print("make file")
def mkdir(one = ""):
print("make drive")
def append(one = "", two = ""):
print("append")
def form(one = ""):
print("format")
def delfile(one = ""):
print("delete file")
def deldir(one = ""):
print("delete directory")
def quit():
print("quit")
sys.exit(0)
The problem seems to be the definition of first(). You invoke it as a function:
if (len(a) == 2) == True: first(a)
elif (len(a) == 3) == True: first(a)
But you define it as a method:
def first(self, a = list()):
The array of command and argument gets put into self and a is always an empty list which you attempt to index and fail. Also, you shouldn't use a mutable type like list() as a default value unless you're certain what you are doing. I suggest simply:
def first(a):
As far as your __main__ code goes, simplify:
if __name__ == '__main__':
for line in sys.stdin:
string = line.strip()
if not string:
break
if string == "quit":
quit()
tokens = string.split()
length = len(tokens)
if 2 <= length <= 3:
first(tokens)
else:
print("Invalid Input. Please Re-enter.")
Real issue:
To solve your error you have to remove the self parameter of the first function
def first(a=list())
Basically the self is only used for object orientation creating methods.
Function like yours can't use self otherwise you will passing the first parameter to self not to a which you want to.
My second issue I can point out is that, You are trying to compare using is between a string and a function.
def first(a = list()):
word = a[0]
if word is "ls":
ls(a[1])
elif word is "format":
format(a[1])
elif word is "reconnect":
reconnect(a[1])
elif word is "mkfile":
mkfile(a[1])
elif word is "mkdir":
mkdir(a[1])
elif word is "append":
append(a[1], a[2])
elif word is "delfile":
delfile(a[1])
elif word is "deldir":
deldir(a[1])
else:
print("Invalid Prompt. Please Re-enter.")
Extra
The is function on built in operations in Python. is compare the equity of the objects.
But this expression:
if (str(s) is "quit") == True:
Can be simpler like:
if str(s) == "quit":
Or:
if str(s) is "quit":
The == True is meaningless either == False you can use not more pythonicly.

Python function stops working after moving it to another file

I have a python code that works how I expect it to, and I want to move the functions send_cmd() to another commands.py file.
import add_pinout
from lib import *
pinout = {}
avr_board = AVRClient('COM3', 250000)
i2c_master = I2CMaster(avr_board)
i2c_slave_adress = 0x1E
i2c_device = I2CDevice(avr_board, i2c_slave_adress)
def send_cmd(cmd):
try:
if cmd[0][:4] == 'LCL_':
if cmd[0] == 'LCL_ALL':
for pin in pinout:
gpio_pin = GPIO(avr_board, pinout[pin], GPIO.Mode.OUTPUT)
if cmd[1] is '1': gpio_pin.write(True)
elif cmd[1] is '0': gpio_pin.write(False)
else:
gpio_pin = GPIO(avr_board, pinout[cmd[1]], GPIO.Mode.OUTPUT)
if cmd[1] is '1': gpio_pin.write(True)
elif cmd[1] is '0': gpio_pin.write(False)
elif cmd[0] == 'WRITE':
i2c_device.data_transfer(cmd[1], len(cmd[1]))
elif cmd[0] == 'READ':
print i2c_device.data_transfer([], len('1010'))
elif cmd[0] == 'WRITE+READ':
print i2c_device.data_transfer(cmd[1], len('1010'))
else:
print 'Something went wrong...'
except KeyError:
print 'Such pin does not exist.'
except IndexError:
print 'Not enough arguments.'
if __name__ == "__main__":
pinout.update(add_pinout.read_pins())
print(pinout)
while(True):
send_cmd(raw_input().split())
I do it by adding from commands.py import send_cmd and addiing arguments to the function. The call looks like this: send_cmd(avr_board, pinout, i2c_device, raw_input().split()), while the declaration in commands.py is def send_cmd(avr_board, pinout, i2c_device, cmd):. When i run the code it accepts user input, but when it calls start_cmd from commands.py, it just goes to the else clause, like it couldnt see the list made by raw_input().split().
Edit
I have found the error - in commands.py I have changed == into is in each if statement. I have reversed this change and now everything's fine.

Appending to a file, then reading from it into a list, then re-appending to it and overwriting certain parts

I want to be able to have a program whereby the user can input a paragraph/sentence/word/character whatever and have that stored in a list e.g. in list[0]. Then I want them to be able to write another bit of text and have that stored in e.g. list[1]. Then at any time I want the user to be able to read that from the list by choosing which segment they want to read from e.g. reading "hello" from list[0] whilst in list[1] "hi" is stored. Then when the user exits the program I want the list to be written to an external file. Then, at next start up, the program should read the file contents and store it again in the list so that the user can add more bits of text or read the current bits. When the list is saved to a file it should append new or changed parts but overwrite parts that are the same so as not to have duplicates. I have attempted this without much success. I am to be honest not sure if it is possible. I have browsed similar forums and have found that hasn't helped much so here it is.
My code so far:
import os
import time
import csv
global write_list
global f1_contents
write_list = []
def write():
os.system("cls")
user_story = input("Enter your text: \n")
write_list.append(user_story)
def read():
os.system("cls")
user_select_needs = True
while user_select_needs == True:
user_select = input("Enter the list section to read from or type exit: \n")
if user_select == "exit":
user_select_needs = False
try:
int(user_select)
select = user_select
select = int(select)
try:
print(write_list[select])
user_select_needs = False
enter = input("Press enter:")
except:
print("There is not stored data on that section!")
except ValueError:
print("That is not a valid section!")
def exit():
os.system("cls")
max_num_needs = True
while max_num_needs == True:
set_max_num = input("Set the storage: \n")
try:
int(set_max_num)
max_num = set_max_num
max_num = int(max_num)
max_num_needs = False
except:
print("It must be an integer!")
for i in range(0, max_num):
f = open("Saves.txt", "a")
f.write(write_list[i])
f.close()
os._exit(1)
def main():
store_num_needs = True
while store_num_needs == True:
set_store_num = input("State the current storage amount: \n")
try:
int(set_store_num)
store_num = set_store_num
store_num = int(store_num)
store_num_needs = False
except:
print("It must be an integer!")
try:
f1 = open("Saves.txt", "r")
for i in range(0, store_num+1):
i, = f1.split("#")
f1.close()
except:
print("--------Loading-------")
time.sleep(1)
while True:
os.system("cls")
user_choice = ""
print("Main Menu" + "\n" + "---------")
print("1) Write")
print("2) Read")
print("3) Exit")
while user_choice not in ["1", "2", "3"]:
user_choice = input("Pick 1, 2 or 3 \n")
if user_choice == "1":
write()
elif user_choice == "2":
read()
else:
exit()
if __name__ == "__main__":
main()
It might be too complicated to understand in which case just ask me in comments- otherwise general tips would be nice aswell.
Thanks in advance
A quick point of correction:
global is only required if you're defining a global variable inside a non-global context. In other words, anything defined at the default indentation level, will be accessible by everything else defined below it. For example:
def set_global():
x = 1
def use_global():
x += 1
set_global()
try:
use_global()
except Exception as e:
# `use_global` doesn't know
# about what `set_global` did
print("ERROR: " + str(e))
# to resolve this we can set `x` to a
# default value in a global context:
x = 1
# or, if it were required, we
# could create a global variable
def make_global():
global x
make_global()
# either will work fine
set_global()
use_global()
print(x) # prints 2
Now to the actual question:
I haven't read through the block of code you wrote (probably best to trim it down to just the relevant bits in the future), but this should solve the problem as I understand it, and you described it.
import os
import sys
user_text = []
# login the user somehow
user_file = 'saves.txt'
def writelines(f, lines):
"""Write lines to file with new line characters"""
f.writelines('\n'.join(lines))
def readlines(f):
"""Get lines from file split on new line characters"""
text = f.read()
return text.split('\n') if text else []
class _Choice(object):
"""Class that is equivalent to a set of choices
Example:
>>> class YesObj(Choice):
>>> options = ('y', 'yes')
>>> Yes = YesObj()
>>> assert Yes == 'yes'
>>> assert Yes == 'y'
>>> # assertions evaluate to True
Override the `options` attribute to make use
"""
allowed = ()
def __eq__(self, other):
try:
s = str(other)
except:
raise TypeError("Cannot compare with non-string")
else:
return s.lower() in self.allowed
def _choice_repr(choices):
allowed = []
for c in choices:
if isinstance(c, _Choice):
allowed.extend(c.allowed)
else:
allowed.append(c)
if len(allowed) > 2:
s = ', '.join([repr(c) for c in allowed[:-1]])
s += ', or %s' % repr(allowed[-1])
elif len(allowed) == 1:
s = '%s or %s' % allowed
else:
s = '%s' % allowed[0]
return s
def _choice_sentinel(name, allowed):
"""Creates a sentinel for comparing options"""
return type(name, (_Choice,), {'allowed': list(allowed)})()
Quit = _choice_sentinel('Quit', ('q', 'quit'))
Yes = _choice_sentinel('Yes', ('y', 'yes'))
No = _choice_sentinel('No', ('n', 'no'))
def readline_generator(f):
"""Generate a file's lines one at a time"""
t = f.readline()
# while the line isn't empty
while bool(t):
yield t
t = f.readline()
def read_from_cache():
"""Overwrite `user_text` with file content"""
if not os.path.isfile(user_file):
open(user_file, 'w').close()
globals()['user_text'] = []
else:
with open(user_file, 'r') as f:
lines = readlines(f)
# replace vs extend user text
for i, t in enumerate(lines):
if i == len(user_text):
user_text.extend(lines[i:])
else:
user_text[i] = t
def write_to_cache():
"""Overwrite cache after the first line disagrees with current text
If modifications have been made near the end of the file, this will
be more efficient than a blindly overwriting the cache."""
with open(user_file, 'r+') as f:
i = -1
last_pos = f.tell()
# enumerate is a generator, not complete list
for i, t in enumerate(readline_generator(f)):
if user_text[i] != t:
# rewind to the line before
# this diff was encountered
f.seek(last_pos)
# set the index back one in
# order to catch the change
i -= 1
break
last_pos = f.tell()
# then cut off remainder of file
f.truncate()
# recall that i is the index of the diff
# replace the rest of it with new
# (and potentially old) content
writelines(f, user_text[i+1:])
def blind_write_to_cache():
"""Blindly overwrite the cache with current text"""
with open(user_file, 'w') as f:
writelines(f, user_text)
def overwrite_user_text(i, text, save=False):
"""Overwrite a line of text
If `save` is True, then these changes are cached
"""
try:
user_text[i] = text
except IndexError:
raise IndexError("No text exists on line %r" % (i+1))
if save:
write_to_cache()
def user_input():
"""Get a new line from the user"""
return raw_input("input text: ")
def user_choice(msg, choices):
if len(choices) == 0:
raise ValueError("No choices were given")
ans = raw_input(msg)
if ans not in choices:
print("Invalid Response: '%s'" % ans)
m = "Respond with %s: " % _choice_repr(choices)
return user_choice(m, choices)
else:
return ans
def user_appends():
"""User adds a new line"""
user_text.append(user_input())
def user_reads(*args, **kwargs):
"""Print a set of lines for the user
Selects text via `user_text[slice(*args)]`
Use 'print_init' in kwargs to choose how
many lines are printed out before user must
scroll by pressing enter, or quit with 'q'."""
print_init = kwargs.get('print_init', 4)
sliced = user_text[slice(*args)]
if not isinstance(sliced, list):
sliced = [sliced]
for i, l in enumerate(sliced):
if i < print_init:
print(l)
sys.stdout.flush()
elif user_choice(l, ['', Quit]) == Quit:
break
def user_changes(i=None, save=False):
"""User changes a preexisting line"""
attempt = True
while i is None and attempt:
# get the line the user wants to change
i_text = raw_input("Line to be changed: ")
try:
# make user input an index
i = int(i_text)
except:
# check if they want to try again
c = user_choice("Bad input - '%s' is not an "
"integer. Try again? " % i_text, (Yes, No))
attempt = (c == Yes)
if attempt:
# user gave a valid integer for indexing
try:
user_reads(i-1)
overwrite_user_text(i-1, user_input(), save)
except Exception as e:
print("ERROR: %s" % e)
if user_choice("Try again? ", (Yes, No)):
user_changes(i, save)
# stores whatever text is already on
# file to `user_text` before use
read_from_cache()

completing a function fully

I AM NEW TO PYTHON AND CODING IN GENERAL.
So I have a program with a menu that has multiple functions in it. individually each function works fine on its own, however when i put them together they would usually not fully execute and instead stop half way or wont work at all.
EXAMPLE- the function remove wont remove what i tell it to.
def show_coffee will only show the first description and weight only and nothing else.
What can i do to make the functions fully execute?
import os
def main():
choice =''
fun=[]
while choice != 4:
menu()
choice=getUserChoice()
if choice !=4:
fun=process_choice(choice,fun)
print(fun)
print("Goodby!")
def process_choice(choice,fun):
#fun=fun1
if choice == 0:
fun=add_coffee(fun)
elif choice == 1:
fun=show_coffee(fun)
elif choice == 2:
fun=search_coffee(fun)
elif choice == 3:
fun=modify_coffee(fun)
else:
print(choice,"is not a valid choice.")
return fun
def add_coffee(fun):
another= 'y'
coffee_file=open('coffee.txt', 'a')
Description={}
while another == 'y' or another == 'Y':
print('Enter the following coffee data:')
descr=input('Description: ')
qty= int(input('Quantity (in pounds): '))
coffee_file.write(descr + '\n')
coffee_file.write(str(qty) + '\n')
print("Do you want to add another record?")
another = input("Y=yes, anything else =no: ")
return fun
coffee_file.close()
print('Data append to coffee.txt.')
def show_coffee(fun2):
coffee_file=open ('coffee.txt', 'r')
descr=coffee_file.readline()
while descr != "":
qty= str(coffee_file.readline())
descr=descr.rstrip('\n')
print('Description:', descr)
print('Quantity:', qty)
descr= coffee_file.readline()
fun=fun2
return fun
coffee_file.close()
def search_coffee(fun3):
found=False
search =input('Enter a description to search for: ')
coffee_file=open('coffee.txt', 'r')
descr=coffee_file.readline()
while descr != '':
qty= float(coffee_file.readline())
descr = descr.rstrip('\n')
if descr== search:
print('Description:', descr)
print('Quantity:', qty)
found=True
descr=coffee_file.readline()
fun=fun3
return fun
coffee_file.close()
if not found:
print('That item was not found in the file.')
def modify_coffee(fun4):
found=False
search=input('Which coffee do you want to delete? ')
coffee_file=open('coffee.txt', 'r')
temp_file=open('temp.txt', 'w')
descr=coffee_file.readline()
while descr != '':
qty=float(coffee_file.readline())
descr=descr.rstrip('\n')
if descr !=search:
temp_file.write(descr + '\n')
temp_file.write(str(qty) + '\n')
else:
found=True
descr=coffee_file.readline()
fun=fun4
return fun
coffee_file.close()
temp_file.close()
os.remove('coffee.txt')
os.rename('temp.txt', 'coffee.txt')
if found:
print('The file has been update.')
else:
print('The item was not found in the file.')
def menu():
print('''
0. Add or Update an entry
1. Show an entry
2. Search
3. remove
4. Remove number
''')
def getUserChoice():
choice=-1
while choice <0 or choice > 3:
print("Please select 0-3: ",end='')
choice=int(input())
return choice
You are defining functions but this does not call a function. The standard way to do this in Python is use the if __name__=="__main__": statement at the bottom of a file. When the file is executed (instead of functions/classes being imported by another script) the code block within the scope of if __name__=="__main__": is executed.
Get comfortable with this, it's useful and clean :) Good read - What does if __name__ == "__main__": do?
So, for example...
At the bottom of your file...
if __name__=="__main__":
main()

Categories