-2

I have this code here the problem I'm facing is that whenever I put in an input like 35 paperbacks and 15 hardbacks it returns the same number for all 4 months.

# Month 1
#         Paperbacks: 35
#          Hardbacks: 15
# Month 2
#         Paperbacks: 35
#          Hardbacks: 15
# Month 3
#         Paperbacks: 35
#          Hardbacks: 15
# Month 4
#         Paperbacks: 35
#          Hardbacks: 15  

I need it to make it so the paperbacks increase by 100 every month and the hardbacks to increase by 25 every month. So, if I were to input 35 paperbacks and 15 hardbacks, it would return:

Month 1: 135 paperbacks and 40 hardbooks

Im aware that you are supposed to use the += operator, but I'm not sure how to use it and implement it into this code.

paperbacks = input('What is the current number of paperbacks? ')
hardbacks = input('What is the current number of hardbacks? ')

# Display the inventory stock table.
for month in range(1, 5):

    print(f'Month {month}')
    print(f'\tPaperbacks: {paperbacks}')
    print(f'\t Hardbacks: {hardbacks}')
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • You only have one month's inventory information, so that's all you can print. Where it would get the other months' information? You need to ask for the number of books *for each month,* and store them in a list or dictionary. Then iterate over that list or dictionary when printing the output. – kindall Sep 02 '22 at 23:23
  • https://stackoverflow.com/questions/4841436/what-exactly-does-do – Peter Wood Sep 02 '22 at 23:27

4 Answers4

3

Issue in the code: The input for paperbacks and hardbacks should be converted to integers as the input function returns strings by default. You can learn more about the input function here.

The += operator: The += operator can be used to update your variables inside the loop.

The operator will add the value on the operator's right to the value on the left. The result is then stored in the variable on the left. E.g. x += y is equivalent to x = x + y.

You can learn more about the addition assignment operator here.

Solution: The code below assumes you wish to update paperbacks by 100 and hardbacks by 25 each month:

paperbacks = int(input('What is the current number of paperbacks? '))
hardbacks = int(input('What is the current number of hardbacks? '))

# Display the inventory stock table.
for month in range(1, 5):
    paperbacks += 100
    hardbacks += 25
    print(f'Month {month}')
    print(f'\tPaperbacks: {paperbacks}')
    print(f'\t Hardbacks: {hardbacks}')
Prins
  • 1,051
  • 1
  • 6
  • 9
  • `+=` is an [assignment operator](https://www.geeksforgeeks.org/assignment-operators-in-python/) – Jess Sep 02 '22 at 23:27
0
paperbacks = int(input('What is the current number of paperbacks? '))
hardbacks = int(input('What is the current number of hardbacks? '))

# Display the inventory stock table.
paperbacks_increase = 100
handbook_increase = 25

for month in range(1, 5):
    print(f'Month {month}')
    print(f'\tPaperbacks: {paperbacks + month * paperbacks_increase}')
    print(f'\t Hardbacks: {hardbacks + month * handbook_increase}')
-1

Im aware that you are supposed to use the += operator, but I'm not sure how to use it and implement it into this code.

for month in range(1, 5):
    paperbacks += 100
    hardbacks += 25
    ...

Is equivalent to:

for month in range(1, 5):
    paperbacks = paperbacks + 100
    hardbacks = hardbacks + 25
    ...

It just makes it so one doesn't have to type the variable twice.

One can use it with other operators as well, like -=, *=, /= and so on.

P.S.

Wrap an int() around your input :)

taylorSeries
  • 505
  • 2
  • 6
  • 18
-1

Firstly, your paperbacks and hardbacks variables are strings so you first have to convert them into an int with int() before you can start treating it like a number such as addition.

You then just add the amount with the += as you wanted inside the for loop to add the amount with every iteration.

This is probably what your after:

paperbacks = int(input('What is the current number of paperbacks? '))
hardbacks = int(input('What is the current number of hardbacks? '))

# Display the inventory stock table.
for month in range(1, 5):
    paperbacks += 100
    hardbacks += 25

    print(f'Month {month}')
    print(f'\tPaperbacks: {paperbacks}')
    print(f'\t Hardbacks: {hardbacks}')