Things got a little backed up - we're processing the data and things should be back to normal within the hour.

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
Python seeming not to respect a while loop? Improper Objects being affected
Post Body

#==== Fruit Class =============================
class Fruit:

    fn = "NOT_SET"
    fp = 0.00

    def __init__(self, fname, fprice=0):
        self.fp = fprice
        self.fn = fname

    def setPrice(self, price):
        self.fp = price

    def getPrice(self):
        return self.fp

    def getName(self):
        return self.fn

#======== Customer Class ====================
class Customer:

    cn = "NOT_SET"

    def __init__(self, cname):
        self.cn = cname

    def getName(self):
        return self.cn

#========= Shoping Cart Class to hold Fruits and Customer Name
class ShopCart(Customer):

    fs = []
    amounts = []

    def __init__(self, cname):
        self.cn = cname

    def addItem(self, fruit, amt):
        self.fs.append(fruit)
        self.amounts.append(amt)

    def getItemPrice(self, fruit):
        return fruit.getPrice()

    def getBalance(self):
        bal = 0
        amt = 0
        i = 0
        while i < len(self.fs):
            bal  = self.fs[i].getPrice()*self.amounts[i]
            amt  = self.amounts[i] 
            i  = 1
        return "{} fruits for ${}".format(amt,bal)

    def getCartInfo(self):
        ret = "Cart: "
        i = 0
        while i < len(self.fs):
            ret  = "\n--{} at {} each.".format(self.fs[i].getName(), self.amounts[i])
            i  = 1
        return ret

#=========== Start of the Script ===========

fruits = []
clients = []

print("Welcome to the Fruit Stand! (Vendor Edition)")
print("Please enter all fruits and their prices.")
print("Enter 'done' when finished.")

inp = "0"
while inp != "done":
    print("")
    nm = input("Fruit Name: ")
    if(nm == "done"):
        break
    pr = int(input("Price: "))
    fruits.append(Fruit(nm,pr))

print("##===============================##")
print("Fruits in the Stand today: ")
for Fruit in fruits:
    print("{} at ${} each.".format(Fruit.getName(), Fruit.getPrice()))

print("##===============================##")
print("Now enter in all the Customers you'll have today (until 'done' is entered)...")
while inp != "done":
    print("")
    nm = input("Client Name: ")
    if(nm == "done"):
        break
    clients.append(ShopCart(nm))

print("##===============================##")
print("Enter in order details...")

i = 0
while i < len(clients):
    print("Enter the Order for {}".format(clients[i].getName()))
    for Fruit in fruits:
        clients[i].addItem(Fruit, input("-- How many {}s : ".format(Fruit.getName())))
    i  = 1

print("##===============================##")
print("ALL ORDERS TO BE FILLED")
for ShopCart in clients:
    print("> {}'s Cart:".format(ShopCart.getName()))
    print(ShopCart.getCartInfo() "\n")

When this runs it works great up until line 105 (running through client list)

it goes through...

but when displaying the 'carts' both client objects are having their addItem called despite not being in the current loop of the while loop/ I'm confused at why this happening. Thanks for any help!!!

EDIT:

example run:

Enter the Order for Bailey
-- How many Apples : 10
Enter the Order for Aiden
-- How many Apples : 10
##===============================##
ALL ORDERS TO BE FILLED
> Bailey's Cart:
Cart:
--Apple at 10 each.
--Apple at 10 each.

> Aiden's Cart:
Cart:
--Apple at 10 each.
--Apple at 10 each.

Author
Account Strength
100%
Account Age
6 years
Verified Email
Yes
Verified Flair
No
Total Karma
2,780
Link Karma
2,364
Comment Karma
363
Profile updated: 3 days ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago