In: Computer Science
Can I get this logic in Python? plus what software I can use it to run?
thanks and will rate!!!
Search Lab
implement the following logic in Python, use appropriate data types. Data types are represented as either numeric (num) or string.
string name
string address
num item
num quantity
num price
num SIZE = 6
num VALID_ITEM [SIZE] = 106, 108, 307, 405, 457, 688
num VALID_ITEM_PRICE [SIZE] = 0.59, 0.99, 4.50, 15.99, 17.50, 39.00
num sub
string foundIt = “N”
string MSG_YES = “Item available”
string MSG_NO = “Item not found”
get name, address, item, quantity
sub = 0
while sub <= SIZE
if item == VALID_ITEM [sub] then
foundIt = “Y”
price = VALID_ITEM_PRICE [sub]
endif
sub = sub + 1
endwhile
if foundIt == “Y” then
print MSG_YES
print quantity, “ at “ , price, “ each”
print “Total “, quantity * price
else
print MSG_NO
endif
Below is your solution please go through it. For your better understanding I made it using class so that you can understand constructor,setter and getters concepts.I have called class using object please go through it. Moreover you can use PyCharm,Spyder,PyDev,Jupyter IDE to run. If you find difficulty use can use any online Python IDE to get output.
SOLUTION
class Logic: #created logic class for setter getter and
constructor
def __init__(self, name,address,item=0,quantity=0,price=0):
#initialization of the variables
self._name=name
self._address=address
self._item=item
self._quantity=quantity
self._price=price
def get_name(self):
return self._name
def get_address(self):
return self._address
def get_item(self):
return self._item
def get_quantity(self):
return self._quantity
def get_price(self):
return self._price
def set_price(self, price):
self._price = price
o1= Logic("",""); #passing name and address as null you can pass
any parameters and can also pass #other variables like
item,quantity and price
SIZE=6
VALID_ITEM = [106, 108, 307, 405, 457, 688]
VALID_ITEM_PRICE =[0.59, 0.99, 4.50, 15.99, 17.50, 39.00]
sub=0
foundIt ="N"
MSG_YES = "Item available"
MSG_NO = "Item not found"
while(sub<=SIZE-1): #whileloop
if(o1.get_item()== VALID_ITEM [sub]):
foundIt = "Y"
o1.set_price(VALID_ITEM_PRICE [sub])
sub = sub + 1
if (foundIt == "Y"):
print (MSG_YES)
print (o1.get_quantity() ," at ", price," each")
sum=quantity*price
print ("Total",sum)
else:
print (MSG_NO)