Issue
I've coded this in the "add items" area of an inventory system I am creating, and I want to send the user back to the "menu" to update the item instead of adding a new item if it is already in the system (e.g. Hulk). Though the current code works for the first name (spiderman). It doesn't for the others.
def add_item(): # If user enters a name already in the system, then they are returned to menu screen and directed to update item instead
try:
with open('data.txt','a') as file:
my_file = csv.writer(file)
Name_ID = input('Please enter the Name: ')
if Name_ID != ('Spiderman' or 'Hulk' or 'Superman' or 'Wolverine' or 'Flash' or 'Obi Wan Kenobi'):
Price_ID = input('Please enter the Price: ')
Quantity_ID = input('Please enter the Quantity: ')
Product_ID = input('Please enter the Product: ')
Brand_ID = input('Please enter the Brand: ')
my_file.writerow([Name_ID, Price_ID, Quantity_ID, Product_ID, Brand_ID])
print('Item Added')
menu_display()
elif Name_ID == ('Spiderman' or 'Hulk' or 'Superman' or 'Wolverine' or 'Flash' or 'Obi Wan Kenobi'):
print('This name already exists, please update items instead')
menu_display()
except:
menu_display()
I know it must be an issue with the "if Name_ID !=" part of the code, but I'm unsure how to change it in a more effective way. I'm new to coding so any help is appreciated.
Here is how the file looks.
Name, Price, Quantity, Product, Brand
Spiderman, 30.50, 3, Comic, Marvel
Hulk, 50, 2, Figurine, Marvel
Superman, 150, 1, Figurine, DC
Wolverine, 20, 2, Comic, Marvel
Flash, 300, 1, Figurine, Dc
Obi Wan Kenobi, 400.99, 1, Figurine, Star Wars
Solution
This happens because your conditional statement is translated by python like this:
if Name_ID != 'Spiderman':
if 'Hulk': # always True
if 'Superman': # always True
if 'Wolverine': # always True
...
Price_ID = input('Please enter the Price: ')
Quantity_ID = input('Please enter the Quantity: ')
...
if "any string":
is always going to be True
unless it's the empty string ""
.
In order to get the desired results you would need to rewrite it to something like this:
if Name_ID != 'Spiderman' or Name_ID != 'Hulk' or Name_ID != 'Superman' ...
But a better alternative would be to make a list and ask if the value exists or doesn't exist in the list:
if Name_ID not in ['Spiderman','Hulk','Superman','Wolverine', 'Flash','Obi Wan Kenobi']:
...
Additionally instead of asking the same thing again in your elif
statement you can instead just put else
.
So your try
branch would end up looking like this:
my_file = csv.writer(file)
Name_ID = input('Please enter the Name: ')
if Name_ID not in ['Spiderman', 'Hulk', 'Superman', 'Wolverine', 'Flash', 'Obi Wan Kenobi']:
Price_ID = input('Please enter the Price: ')
Quantity_ID = input('Please enter the Quantity: ')
Product_ID = input('Please enter the Product: ')
Brand_ID = input('Please enter the Brand: ')
my_file.writerow([Name_ID, Price_ID, Quantity_ID, Product_ID, Brand_ID])
print('Item Added')
menu_display()
else:
print('This name already exists, please update items instead')
menu_display()
Answered By - alexpdev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.