0

I'm looking for a more 'dumbed-down' version of a solution to this issue I have. Below is the code I have.

I'm trying to allow for both the % symbol and simply a number as input for the percentage from user.anyone that can help me would have to write this out for me more than the previous examples explain please.

#104 Percentages
print('#104 Percentages')
percent = float(input('Enter percentage: '))
decimal = percent/100
print('Equivalent decimal:',round(decimal,2))
print()
Community
  • 1
  • 1
  • You can `input(...).strip('%')` to allow for `%` or not. And just format the output rather than `round()`, e.g. `print('Equivalent decimal: {:.2f}'.format(decimal))`. But isn't this already answered in the post you've linked to? – AChampion Dec 01 '16 at 02:36
  • Possible duplicate of [What is a clean way to convert a string percent to a float?](http://stackoverflow.com/questions/12432663/what-is-a-clean-way-to-convert-a-string-percent-to-a-float) – AChampion Dec 01 '16 at 02:37
  • Thank you! I appreciate your feedback. ShadowRanger (shown below) fixed me up good with code i could use verbatm. – jwjoseph82 Dec 02 '16 at 05:44

1 Answers1

2

The question you linked has answers describing the use of str.strip (though rstrip would be more appropriate) to remove the trailing % if it exists. Just do that on the return from input before you pass it to float, changing the line to:

percent = float(input('Enter percentage: ').rstrip('%'))

If you'd like it split up for clarity:

maybe_percent_sign_str = input('Enter percentage: ')
no_percent_sign_str = maybe_percent_sign_str.rstrip('%')
percent = float(no_percent_sign_str)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thank you so so much ShadowRanger! You've solved my issue. I appreciate the way you answered this...Excellent! – jwjoseph82 Dec 02 '16 at 05:45