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.