I am trying to figure out a way to detect a string input of let's say 54% and convert that to decimal. I can get the percentage to convert with a simple if() statement and multiplying it by a 100. I just can't figure out what I should set the if statements argument to?
Asked
Active
Viewed 163 times
-2
-
Can you share your code even if it does not work correctly? Also, you can add sample inputs and outputs for your program. – Alperen Jul 20 '20 at 19:10
-
`"%" == "%"`, although if you want to print 50% as `"%d %" % 50` it will raise ValueError you have to use `"%d %%" % 50` – Grijesh Chauhan Jul 20 '20 at 19:11
-
Does this answer your question? [Replacing instances of a character in a string](https://stackoverflow.com/questions/12723751/replacing-instances-of-a-character-in-a-string) – Roim Jul 20 '20 at 19:15
-
Does this answer your question? [How to check a string for specific characters?](https://stackoverflow.com/questions/5188792/how-to-check-a-string-for-specific-characters) – Gino Mempin Jul 21 '20 at 00:07
3 Answers
2
lst = ['54%', '54', '0.37%', 'apple%']
# Go through the list
for i in lst:
# Check whether there's a percent sign
if '%' in i:
# Try whether only taking away the % sign is enough for it to be able to be divideable
try:
# Take out that % sign
percent = float(i.replace('%', ''))
decimal = percent / 100
print(decimal)
# If taking the % sign away is not enough, do the following stuff
except:
# Create an empty list which we will append to later and set a variable to 0
lstnumbers = []
errors = 0
# Go through every character of the item
for numbers in i:
# Check whether it can be changed to an integer and append it to a list
try:
number = int(numbers)
lstnumbers.append(number)
# Increase the variable by 1 if there's a charachter that can't be converted to an integer
except:
errors += 1
# If there's something with a % that can't be converted to an integer, print that
if errors > 0:
print("there's a value with a % sign, which cannot be converted:", i)
# If there were some integers in the item, do something
if len(lstnumbers) > 0:
# Replace the first 0 as it the input could also be 0.46 for example and 046 would be no number we'd be interested in
lstnumbersnew = ['' if lstnumbers[0] == '0' else number for number in lstnumbers]
# Bring the items from the list together so we have string
finalnumber = ''.join(str(lstnumbersnew))
# Make that string an integer and divide it
finalintegers = int(float(finalnumber)) / 100
# Print it or do whatever you want to do with it
print(finalintegers)
This will output:
0.54
0.0037
there's a value with a % sign, which cannot be converted: apple%

Jem
- 557
- 9
- 28
-
```percent = int(i.replace('%', ''))``` this will break if there are other characters present, like in '.54%' – M-Wi Jul 20 '20 at 19:20
-
@M-Wi You were absolutely right, my previous code was very easy to break. It took some time but I enhanced my code which should make it capable handling a lot more diverse input. – Jem Jul 20 '20 at 19:49
0
input = input(': ')
percent = (% in input)
if percent == True:
#Do what you want
print('Correct')
else:
print('Wrong')

Plum The Coder
- 51
- 4
-
-
1And it's a bad idea to replace the built-in `input` function with a variable `input`. – Gino Mempin Jul 21 '20 at 00:15
0
You can use an in line if like this:
def detectInput(inp, charToDetect): # inp = the user input
# if % in inp then convert else do nothing
print(((int(inp.replace(charToDetect, '')))/100) if (charToDetect in inp) else "")
x = "54%"
detectInput(x, '%')

tibi
- 186
- 3
- 11