Home > Back-end >  How to properly use the = to get the correct output
How to properly use the = to get the correct output

Time:09-03

I have this code here and basically, the problem I'm facing is that whenever I put in an input like 35 paperbacks and 15 hardbooks it returns the same number for all 4 months. I need it to make it so the paperbacks increase by 100 every month and the hardbooks to increase by 25 every month so if I were to input 35 paperbacks and 15 hardbooks it would return
Month 1: 135 paperbacks and 40 hardbooks

Im aware that you are supposed to use the = operator but Im 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}')```

CodePudding user response:

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 left to the value on the right. 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}')

CodePudding user response:

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}')

CodePudding user response:

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 :)

CodePudding user response:

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}')
  • Related