Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
Hello everyone I'm writing a basic inventory management system and in one of my functions I'm trying to print specific values within the text document for the function trying to print user names, numbers and users designations I keep returning
UnboundLocalError: local variable 'user_numbers' referenced before assignment
Here's my code:
def display_users():
all_users = open('all_users.txt', 'r')
user_description = all_users.readline()
print('All Current User')
print('<----------------->')
while user_description != '':
user_names = all_users.readline()
user_numbers = user_numbers.rstrip('\n')
user_desig = user_desig.rstrip('\n')
print('User Names: ', user_names)
print('User PH Numbers: ', user_numbers)
print('User Designations: ', user_desig)
print('<---------->')
user_description = all_users.readline()
all_users.close()
Edit: I've made it a global variable and now it's just saying
"NameError: name 'user_numbers' is not defined"
https://pastebin.pl/view/ae461557
Here's full code
The issue is with user_numbers = user_numbers.rstrip('\n'), specifically the second part. The error gives you a clear description: user_numbers is not defined yet, so you cannot call rstrip on it. Did you mean to type user_names.rstrip('\n')?
The problem is with this line
user_numbers = user_numbers.rstrip('\n')
In the first iteration of your loop, user_numbers has no value so you cannot call user_numbers.rstrip('\n') on it. I don't follow exactly how you are structuring your text file, but you probably want to make a user_numbers = all_users.readline() call first.
Related
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 2 days ago.
Improve this question
In my make_list_of_free_fields function, I put in a global variable (freesquares), but when I run the program, the draw_move function says I have a NameError, and that freesquares isn't defined. I'm wondering if I should put the global variable in the beginning of the program?
def make_list_of_free_fields(board):
global freesquares
freesquares = []
for row in range(0,3):
for col in range(0,3):
if board[row][col] == 'X' or board[row][col] == 'O':
pass
else:
freesquares.append(([row],[col]))
print(freesquares)
def draw_move(board):
while True:
col, row = randrange(3), randrange(3)
if ([row], [col]) not in freesquares:
continue
else:
board[row][col] = 'X'
return
I watched a tutorial to see if I had everything correct, and the code was pretty much the same, so I'm confused on why this is happening.
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 10 months ago.
Improve this question
I'm having a problem with the following piece of code:
decision = str(input("Would you like to change it?:"))
if decision.lower == 'yes':
new_holiday = input("What is your new favorite holiday?:")
The problem with this is that when I input 'yes' in the first prompt, instead of showing me the second one as I want, it just skips the if statement completely. Am I missing something here?
decision.lower generates a method <built-in method lower of str object at ...>. you should call decision.lower().
Change your code to the following:
decision = str(input("Would you like to change it?:"))
if decision.lower() == 'yes':
new_holiday = input("What is your new favorite holiday?:")
decision.lower() user this method which will return a lower cased string.
decision = input("Would you like to change it?:")
if decision.lower() == 'yes':
new_holiday = input("What is your new favorite holiday?:")
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 2 years ago.
Improve this question
So I've been trying to learn Python and after some easy codes (or whatever they are called) I am now trying to recreate a drinkinggame i always play with some friends. But for that i need this function to return a value which it doesn't do. Can anyone help me? (sorry for the bad English)
def throw():
dice = 6
List = []
i = 0
while i <= dice - len(List):
i +=1
List.append(random.randint(1,6))
return List
print(throw(List))
Your function isn't receiving any parameter, so don't pass anything.
import random
def throw():
dice = 6
List = []
i = 0
while i <= dice - len(List):
i +=1
List.append(random.randint(1,6))
return List
print(throw()) # here
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 6 years ago.
Improve this question
This is all of the code in my while loop. I have declared the num outside the def statement jest so I don't get a global variable error.
Do you need to call all of the other def statements back to the loop?
num = 0;
def Loop(NumOfFeedback, num):
while num < NumOfFeedback:
classOne(Math,Science,English,History,Band,Choir,Spanish,French,German)
workloadOne(Little,Medium,aLot)
classTwo(MathA,ScienceA,EnglishA,HistoryA,BandA,ChoirA,SpanishA,FrenchA,GermanA)
workloadTwo(LittleA,MediumA,aLotA)
GPA(FourPO,ThreePFive,ThreePO,TwoPFive,TwoPO)
Age(A14,B15,C16,D17,E18)
num = num + 1
print num
You are only defining Loop but you are probably not running it.
When you def a function you just save it, but in order to run it you have to call it from your code.
add this at the end of your code (with the values you need):
Loop([NumOfFeedback], num)
You should change the name of the function to be lowercased (and other than just loop - super not clear).
Also move num = 0 down - right above the call.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a list of teamnames. Let's say they are
teamnames=["Blackpool","Blackburn","Arsenal"]
In the program I ask the user which team he would like to do stuff with. I want python to autocomplete the user's input if it matches a team and print it.
So if the user writes "Bla" and presses enter, the team Blackburn should automatically be printed in that space and used in the rest of the code. So for example;
Your choice: Bla (User writes "Bla" and presses enter)
What it should look like
Your Choice: Blackburn (The program finishes the rest of the word)
teamnames=["Blackpool","Blackburn","Arsenal"]
user_input = raw_input("Your choice: ")
# You have to handle the case where 2 or more teams starts with the same string.
# For example the user input is 'B'. So you have to select between "Blackpool" and
# "Blackburn"
filtered_teams = filter(lambda x: x.startswith(user_input), teamnames)
if len(filtered_teams) > 1:
# Deal with more that one team.
print('There are more than one team starting with "{0}"'.format(user_input))
print('Select the team from choices: ')
for index, name in enumerate(filtered_teams):
print("{0}: {1}".format(index, name))
index = input("Enter choice number: ")
# You might want to handle IndexError exception here.
print('Selected team: {0}'.format(filtered_teams[index]))
else:
# Only one team found, so print that team.
print filtered_teams[0]
That depends on your usecase. If your program is commandline based, you can do that at least by using the readline module and pressing TAB. This link also provides some well explained examples on that since its Doug Hellmanns PyMOTW. If you are trying that via a GUI it depends on the API you are using. In that case you need to deliver some more details please.