OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to ignore special characters from input result in Python?

  • Thread starter Thread starter Mae Twost
  • Start date Start date
M

Mae Twost

Guest
I'm a beginner when it comes to Python and I'm trying to write code that adds typed in numbers to a list.

Code:
box = [int(x) for x in input("Type numbers here: ").split()]

box.sort(reverse=False)

print(box)

This code works fine but gets ValueError whenever there is any special character added. Meaning you need to type numbers with spaces between them and if there is a coma added it just doesn't work. Which isn't ideal because it's not allowing float numbers and makes typing characters in rather clunky.

I was trying to change type to float to allow numbers such as 2,5 and to experiment with 're.sub()' but it doesn't really work for me and I am not sure how to proceed with it.
<p>I'm a beginner when it comes to Python and I'm trying to write code that adds typed in numbers to a list.</p>
<pre><code>box = [int(x) for x in input("Type numbers here: ").split()]

box.sort(reverse=False)

print(box)
</code></pre>
<p>This code works fine but gets ValueError whenever there is any special character added. Meaning you need to type numbers with spaces between them and if there is a coma added it just doesn't work. Which isn't ideal because it's not allowing float numbers and makes typing characters in rather clunky.</p>
<p>I was trying to change type to float to allow numbers such as 2,5 and to experiment with 're.sub()' but it doesn't really work for me and I am not sure how to proceed with it.</p>
 
Top