0

I am calculating the ord values of the user input and adding the value of each letter of the word they enter. I am trying to divide the sum (of the word value) by 100 but when I do that, it just prints out as 0.00. Is there an order issue or my function is wrong?

key_word = "quit"
word = ""

while word != key_word:

    word = input(str("Enter a word: "))
    word = word.lower()
    total = float(0)
    cent_value = float(100)
    for letter in word:
        value = ord(letter)
        character = float(value - 96)
        total += character
        cent_value = cent_value / total

    print("Your word is worth $", "{0:.2f}".format(cent_value), sep="")
if word == key_word:
    print(end="")
Mert Köklü
  • 2,183
  • 2
  • 16
  • 20
  • You should debug your code - `print(cent_value)` inside the loop or set breakpoints/inspect variables. At the end you print the centvalue witch starts at 100 and is divided by many `totals` (a value that is constantly increasing) many times - your logic is flawed – Patrick Artner Oct 14 '19 at 20:42
  • 2
    Possible duplicate of [Limiting floats to two decimal points](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – Patrick Artner Oct 14 '19 at 20:47
  • `cent_value = cent_value / total` shouldn't be in the loop, and it should be `cent_value = total / 100` – Barmar Oct 14 '19 at 20:54
  • There's also no need to convert to `float()`. – Barmar Oct 14 '19 at 20:55
  • @PatrickArtner He's already formatting correctly. The problem is that he's calculating the wrong result. – Barmar Oct 14 '19 at 20:56
  • I fixed my issue through the link you attached (Thank you). But just to be clear why was 'cent_value' not able to divide by the total? Is it because cent value is being replaced once it is calculated? @PatrickArtner – Elisabeth Oct 14 '19 at 20:58

1 Answers1

0

It's printing 0.00 because your cent_value ends up being a really small number (roughly 1.7e-06 for your 'value' example), as you're dividing it by an increasing total on each iteration of the loop. The final cent_value ends up being rounded down to 0.00 when formatted.

R001W
  • 71
  • 3