Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 String index out of range - help please
I'm new to programming btw.
I'm trying to write a program that will take a tuple of a list of toys, a tuple of the base prices for those toys, and calculate a 40% mark up and the final price (mark up + base) to get the retail price and then print the toys in one column, the base costs in one column, the mark up in one column, and the final price in one column. Here is what it should look like

Here is my program, which currently gets a string index out of range error and prints it incorrectly. Please help.

#Variable List
# toys - the tuple for the list of toys
# costs - the tuple for the costs of each toy
# mark - the 40 percent mark up
# retail - the base cost plus mark up
# count - the counter/sentry

print ("Welcome to the Mark Up program")
#step 1 - this will set up the toys tuple and the base cost of each 
toys = ("Teddy Bear","Toy Train","Hoola Hoop","Betsy Wetsy","Pogo stick")
costs = (12.50,58.75,10.00,15.00,11.00)

#step 2 -  set up the for loop to calculate mark up on each toy
print ("Item","\t\t","Cost","\t\t","Mark Up","\t\t","Retail")

#step 3 - this will print and format the output

for num in range(len(costs)):
    mark = num * .4
    retail = mark + num
    costs = str(costs)
    mark = str(mark)
    retail = str(retail)
    print (toys[num],"\t",costs[num],"\t",mark[num],"\t",retail[num])
What does print(str(costs)) print? How about print(str(mark))? And finally what does print(str(retail)) print?

Are the printed values what you expected to see?
Sadly it doesn't.
When I put in
toys = ("Teddy Bear","Toy Train","Hoola Hoop","Betsy Wetsy","Pogo stick")
costs = (12.50,58.75,10.00,15.00,11.00)
print ("Item","\t\t","Cost","\t\t","Mark Up","\t\t","Retail")
for num in range(len(costs)):
    mark = num * .4
    retail = mark + num
    costs = str(costs)
    mark = str(mark)
    retail = str(retail)
I got this:
Item Cost Mark Up Retail
(12.5, 58.75, 10.0, 15.0, 11.0)
(12.5, 58.75, 10.0, 15.0, 11.0)
(12.5, 58.75, 10.0, 15.0, 11.0)
(12.5, 58.75, 10.0, 15.0, 11.0)
(12.5, 58.75, 10.0, 15.0, 11.0)
Does that make sense to you? Shouldn't markup be computed from cost instead of how many times you have executed the loop? Shouldn't cost be the cost of one item instead of the cost of all the items?
I agree that markup should be computed from cost not from how many times you have executed the loop, but it needs to calculate the individual 40% mark up of each of the toys and then display them in columns. Btw it doesn't need the chart it just has to be organized columns like the picture
But still doesnt work sadly :(
For the formatting, I first made a function to find which toys' name has the most letters and how many that is. Then I recorded the largest for each. Finally, I went through the cost for each and printed it out. This could definitely be done more smoothly with the help of dictionaries and possibly more functions, which I would suggest to work on. Anyway, here's the code.
def LargestCharacterCount(WordList):
    count = 0
    for word in WordList:
        if len(str(word)) > count:
            count = len(str(word))
    return count

toys = ("Teddy Bear","Toy Train","Hoola Hoop","Betsy Wetsy","Pogo stick")
costs = (12.50,58.75,10.00,15.00,11.00)
markup = ()
for cost in costs:
    markup += (cost*.4, )

LargestToyCharacterAmount = LargestCharacterCount(toys)
LargestCostCharacterAmount = LargestCharacterCount(costs)
LargestMarkupCharacterAmount = LargestCharacterCount(markup)

count = 0
print("Item", " "*(LargestToyCharacterAmount-4), "Cost", " "*(LargestCostCharacterAmount-4), "Markup", " "*(LargestMarkupCharacterAmount-6), "Retail")
for toy in toys:
    print(toy, " "*(LargestToyCharacterAmount-len(toy)), costs[count], " "*(LargestCostCharacterAmount-len(str(costs[count]))), markup[count], " "*(LargestMarkupCharacterAmount-len(str(markup[count]))), costs[count]+markup[count])
    count += 1
DudleyDiccle likes this post
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Thank you so much it worked!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Get the codemap combinations but got tuple index out of range haganmao 1 155 Feb-21-2020, 02:51 AM
Last Post: michael1789
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 610 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  How to get the index of a character from a string chihaya 1 180 Dec-03-2019, 12:54 PM
Last Post: buran
  Unicode string index problem luoheng 6 263 Nov-23-2019, 03:04 PM
Last Post: luoheng
  IndexError: List index out of range issue Adem 1 800 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  IndexError: list index out of range bug? CaptainCsaba 4 1,514 Oct-18-2019, 12:38 PM
Last Post: CaptainCsaba PRGM:IndexError: list index out of range bcramer 0 259 Aug-28-2019, 04:45 PM
Last Post: bcramer
  IndexError: string index out of range ? Q_Y 5 1,345 Jul-05-2019, 07:18 PM
Last Post: noisefloor
  index out of range rwahdan 3 723 Jul-04-2019, 08:11 PM
Last Post: ichabod801
  Preventing: IndexError: list index out of range PappaBear 1 3,520 Jun-03-2019, 05:50 PM
Last Post: SheeppOSU

Forum Jump:

Users browsing this thread: 1 Guest(s)