-3

Super beginner here.

I'm following along the Automate the Boring Stuff With Python book and I decided to make a little script to help me out with some basic percentage checking. I didn't want to open Excel everytime I wanted to do this.

So the script gets two inputs, an old price and a new price and then calculates the percentage change in price. I think I got that right.

The problem occurs once I try to enter a float (that's the right term, yeah?) and I use the comma here in Europe.

I found a topic here where a similar question was answered, but there seems to be an issue on whether or not to call setlocale and (as far as I understand it) it does not deal with how to convert an input?

My code is below:

    def izracun_odstotkov():    #function to calculate the difference in % between original price and new price
    while True:
        try:
            prvotna_cena = float(input('Prosim vnesi prvotno ceno:')) #original price
        except ValueError:
            print('Oprosti, to ni veljavni podatek. Vnesi stevilko.')
            continue

        if prvotna_cena == 0:
                print('Prvotna cena ne more biti 0.')
        else:
            break

    while True:
        try:
            nova_cena = float(input('Prosim vnesi novo ceno:')) #new price
        except ValueError:
            print('Oprosti, to ni veljavni podatek. Vnesi stevilko.')
            continue
        else:
            break

    print(round((float (nova_cena)- float (prvotna_cena))/ float (prvotna_cena)*100, 2), '%')

while True:
    izracun_odstotkov() #This makes the script run over and over so the user can just keep checking the % changes.
Coercer_
  • 39
  • 7
  • 1
    Maybe you can directly replace commas with a dot using something like `float(input(some_prompt).replace(",", "."))`? I'm sure there are more elegant ways to do this, but this hack should also do the job. – Jake Tae May 29 '20 at 11:13
  • Oh, that's true. What do you mean with some_prompt, though? Sorry, I'm still getting used to everything. – Coercer_ May 29 '20 at 11:15
  • 1
    Oh, I just used that as a placeholder for things like `'Prosim vnesi prvotno ceno:'` you had in your example. – Jake Tae May 29 '20 at 11:16
  • Hello again. The. replace method works great for the input. Now I tried to figure out how to make the script output the percentage result with the same replacement and keep getting erros like AttributeError: 'float' object has no attribute 'replace' 'function' object has no attribute 'replace' The only reason I would want to replace it there as well is so I can use pyperclip to deposit the result in the clipboard and then paste it into whichever field I need in another program. Do you have any suggestions on how to solve this? Sorry :( – Coercer_ Jun 01 '20 at 07:28
  • @JakeTae Sorry, forgot to tag you in my comment above. – Coercer_ Jun 01 '20 at 08:05
  • Um, I just figured it out. Here's what I came up with: ```odstotki=round((float (nova_cena)- float (prvotna_cena))/ float (prvotna_cena)*100, 2) print(str(odstotki).replace('.',','), '%') while True: izracun_odstotkov() #This makes the script run over and over so the user can just keep checking the % changes.``` – Coercer_ Jun 01 '20 at 08:09

1 Answers1

2

You can use the replace method:

prvotna_cena = float(input('Prosim vnesi prvotno ceno:').replace(',','.'))
Red
  • 26,798
  • 7
  • 36
  • 58
  • Thank you. This does get suggested a lot. I guess it's the easiest way to do it for a script like this! :) – Coercer_ May 29 '20 at 20:05