0
ItemNumber = []
ItemDescription = []
ItemReserveprice = []
ItemBids = []
Item = 0
Buyerno = 1
BuyerNumber = []
sellercount = 0
whoareyou = input("Are you a seller or a buyer? enter s/b ")
if whoareyou == "s":
   choice1 = input("Do you want to enter your seller ID?: ")
   if choice1 == "yes":
      testItemNumber = input("Please enter your seller id in the form 00N:  ")
      for i in ItemNumber:
        if i == testItemNumber:
          print("Please enter another seller ID")
          ItemNumber.insert(sellercount,int(input("Enter your seller ID again, please make sure it is in the form 00N:  ")))
          while Item < 11:   
            ItemDescription.insert(Item,input("Enter your item description:  "))
            ItemReserveprice.insert(Item,int(input("Enter your reserve price: ")))
            ItemBids.insert(Item,0)
            Item = Item + 1
    if choice1 == "no":
      ItemNumber.insert(sellercount,('00' + str(sellercount+1)))
      print (ItemNumber[sellercount],"is your seller ID")
      while Item < 10:
        print ("Now enter Item number",Item+1)
        ItemDescription.insert(Item,input("Enter your item description:  "))
        ItemReserveprice.insert(Item,int(input("Enter your reserve price: ")))
        ItemBids.insert(Item,0)
        Item = Item + 1
print ("Auction Starts!")
print ("You are now in the buyer screen")
BuyerNumber.insert(Buyerno,('aa' + str(Buyerno)))
print ("Your buyer number is",BuyerNumber)
or i in range (0,10):
  print ("The Item number of item number ",i+1,"is",ItemNumber[i],"The Description is",ItemDescription[i],"The reserve price is",ItemReserveprice[i])
choice2 = input("What item do you want to bid for?, enter the item number")

if i try to print any element from the array that is not the first one i get that error. For example if i try to print ItemNumber[2] or anything above so i cant print ItemNumber[i] as when i goes 2 to i get that error. I'm trying a task for an auction specifically the 2019 June Computer science pre release paper.Im a beginner so please dont bash this is my 2nd month. PS: the code is not completed

  • Many of the lists defined in the first few lines are empty, so you get IndexError – PCM Nov 24 '21 at 02:41

1 Answers1

0

Because in the code

ItemNumber.insert(sellercount,('00' + str(sellercount+1)))

only 1 record has been added into ItemNumber.

So access ItemNumber[i] when i in (2..10) will cause exception.

I try to modify the code, add the item number into ItemNumber as below:

ItemNumber = []
ItemDescription = []
ItemReserveprice = []
ItemBids = []
Item = 0
Buyerno = 1
BuyerNumber = []
sellercount = 0

# NOTE: use SellerNumber to store the seller number
SellerNumber = []

whoareyou = input("Are you a seller or a buyer? enter s/b ")
if whoareyou == "s":
   choice1 = input("Do you want to enter your seller ID?: ")
   if choice1 == "yes":
      testItemNumber = input("Please enter your seller id in the form 00N:  ")
      for i in ItemNumber:
        if i == testItemNumber:
          print("Please enter another seller ID")
          ItemNumber.insert(sellercount,int(input("Enter your seller ID again, please make sure it is in the form 00N:  ")))
          while Item < 11:   
            ItemDescription.insert(Item,input("Enter your item description:  "))
            ItemReserveprice.insert(Item,int(input("Enter your reserve price: ")))
            ItemBids.insert(Item,0)
            Item = Item + 1
   if choice1 == "no":
     # NOTE: insert the seller number
     SellerNumber.insert(sellercount,('00' + str(sellercount+1)))
     print (SellerNumber[sellercount],"is your seller ID")
     while Item < 10:
       print ("Now enter Item number",Item+1)
       # NOTE: insert the item number into ItemNumber
       ItemNumber.insert(Item, 'ITEM_NUMBER' + str(Item))
       ItemDescription.insert(Item,input("Enter your item description:  "))
       ItemReserveprice.insert(Item,int(input("Enter your reserve price: ")))
       ItemBids.insert(Item,0)
       Item = Item + 1
print ("Auction Starts!")
print ("You are now in the buyer screen")
BuyerNumber.insert(Buyerno,('aa' + str(Buyerno)))
print ("Your buyer number is",BuyerNumber)
for i in range (0,10):
  print ("The Item number of item number ",i+1,"is",ItemNumber[i],"The Description is",ItemDescription[i],"The reserve price is",ItemReserveprice[i])
choice2 = input("What item do you want to bid for?, enter the item number")

And it works:

enter image description here

cg-zhou
  • 528
  • 3
  • 6
  • thanks! now tho however i want to add if whoareyou == "b": then continue from the buyer page, how do I make the 2 loops from the s also lead to the buyer page – user17493633 Nov 25 '21 at 06:55
  • A small suggestion is to split the code into multiple functions, and then observe the input and output. It is not easy to merge a lot of codes together for debugging. – cg-zhou Nov 26 '21 at 02:22