0

I have got to shorten the number that comes out of a calculation down to the smallest possible whilst being easy to read, such as 999,000 - 999k, and 999,000,000 - 999m.

I believe I have the code I need but I am not sure how I am meant to implement it into my code

while True: 
    bands  = input("Please enter your first band colour: ").upper() 
    print("")
    if bands== 'BLACK': 
        print("The colour black may only be entered in the 2nd or 3rd colour slots.")
        continue 
    bands1 = input("Please enter your second band colour: ").upper()
    print("")
    bands2 = input("Please enter your third band colour: ").upper()
    print("")

    print("Ohm value of resistor:", (dict[bands]*10 + dict[bands1]) * (10 ** dict[bands2]),"Ω", )
def print_prefix(value):
    prefix=('', 'K', 'M', 'G')
    i = 0
    if (1000<bands//1000**i):
        i=i+1
        print(str(value/(1000**i))+prefix[i])

I expect the result of it to be shortened so a prefix can be added

PunOko7
  • 1
  • 1
  • 1
    Possible duplicate of [What does it mean to "call" a function in Python?](https://stackoverflow.com/questions/19130958/what-does-it-mean-to-call-a-function-in-python) – Ricky Kim May 27 '19 at 21:17
  • PunOko7: What's wrong with the code you have—please [edit] your question and describe what's not working. – martineau May 27 '19 at 21:19
  • 1
    Am I the only one that’s wondering why it’s called prefix if it comes after the number? That’s a suffix my friend. – Jab May 27 '19 at 21:26

1 Answers1

0

Well it should be possible with using the code below:

def print_prefix(value):
    prefix=('', 'K', 'M', 'G')
    i = 0
    while(value >= 1000):
        value = value/1000
        i += 1

    print("{} {}".format(value, prefix[i]))

Basically You are dividing the number while it is larger or equal than 1000. Since each of the letters K, M, G means the number 1000 times bigger than previous. This allows You to find which letter should you use and create the shortened number at the same time.

Dominik Wosiński
  • 3,769
  • 1
  • 8
  • 22