2

The below is my code, but I can't get the last few lines to print. I've tried rearranging them, switching from string to float, etc. I don't know if this is just a glitch or a concatenation problem. If so, how would I make the last few lines print with float?

N = []
C= []
Holiday = []
totC = 0
name = input("enter name").upper()
while name != "XXX":
    cost = int(input("enter amount to spend >0 and <=10"))
    while cost <=0 or cost >10:
        print("invalid cost")
        cost = int(input("enter amount to spend >0 and <=10"))
    gift = name + " " + str(cost)
    N.append(name)
    C.append(cost)
    Holiday.append(gift)
    totC = totC + cost
    name = input("enter name").upper()

print(Holiday)
print(N)
print(C)
print(totC)
print("Total cost is "  + totC)
print("Average cost is" + av)
print("Number of names is " + len(N))
print("Number of costs is " + len(C))
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Julia
  • 1
  • 4

3 Answers3

1

Forget about casting all those values to strings. You don't need to do that. Instead, write your print statements like:

print("Total cost is", totC)
print("Average cost is", av)
print("Number of names is", len(N))
print("Number of costs is", len(C))

Alternatively, if you're using Python 3.6 or newer, you can use f-strings like:

print(f"Total cost is {totC}")
print(f"Average cost is {av}")
print(f"Number of names is {len(N)}")
print(f"Number of costs is {len(C)}")
Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
0

You'll likely have to convert the floats and integers to strings using the str function.

For example, print("Number of costs is " + len(C)) would become print("Number of costs is " + str(len(C))) and print("Total cost is " + totC) would become print("Total cost is " + str(totC))

The print statements should look like this.

print("Total cost is "  + str(totC))
print("Average cost is" + str(av))
print("Number of names is " + str(len(N)))
print("Number of costs is " + str(len(C)))

One thing I noticed is that you don't have a variable called av, so trying to print it would not work. If you're trying to print the average costs, then you can do sum(C)/len(C). So, you can either add av = sum(C)/len(C) right before the print statements, or you can integrate into the print statement, like this:

print("Average cost is" + str(sum(C)/len(C)))
Axiumin_
  • 2,107
  • 2
  • 15
  • 24
0

Use str:

print("Total cost is "  + str(totC))
print("Average cost is" + str(av))
print("Number of names is " + str(len(N)))
print("Number of costs is " + str(len(C)))

You might be confusing concatenation with "addition" of strings and integers.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79