Accept Commas and Spaces In User Input
As suggested in comment, just replace comma ,
by space character
before splitting the input string.
def get_dataset():
while True:
try:
dataset = [float(_) for _ in input("\nEnter Dataset: ").replace(',', ' ').split()]
except ValueError:
print("\nInvalid Input")
continue
if len(dataset) < 2:
print("\nPlease enter at least 2 values.")
else:
return dataset
SAMPLE RUN
>>> print(get_dataset())
Enter Dataset: >? 12,3,4
[12.0, 3.0, 4.0]
One space is added in the beginning of input string
The comma is causing an extra space before the user input string. Replace the comma in the print statement with +
sign.
UserInput = input('Enter a string: ')
print("\nEntered string is:\n" + UserInput)
Output:
Enter a string: I need this, but not this
Entered string is:
I need this, but not this
Related Topics
Django Model Choice Option as a Multi Select Box
Python - Remove Any Element from a List of Strings That Is a Substring of Another Element
Element That Appear More That Once in the List in Python
Python: String Iteration Replace a Space With a Hyphen (Or Other Character)
Convert a Python Int into a Big-Endian String of Bytes
Python Data Frame How to Find the Local Maximum in a 2D Array
Replace Single Quote to Double Quote Python Pandas Dataframe
Key Error: None of [Int64Index...] Dtype='Int64] Are in the Columns
How to Check If a String Contains 2 of the Same Character
How to Get Elasticsearch to Perform an Exact Match Query
How to Split by Commas That Are Not Within Parentheses
How to Make Multiple Empty Lists in Python
Split Large Text File(Around 50Gb) into Multiple Files
How Can One Modify the Outline Color of a Node in Networkx
How to Change Milliseconds to Seconds in Python