I'm trying to add values to a list, and stop adding with empty line:
def main():
signal = []
i = 0
print("Enter the data points of the signal. Stop with empty line.\n")
while i != "":
value = float(input())
signal.append(value)
i = 1
print(signal)
main()
However, when I press enter (empty line) I get the following error:
File "C:\Users\Omistaja\Downloads\template_median_filter.py", line 30, in main
value = float(input())
ValueError: could not convert string to float: ''
How to proceed?
CodePudding user response:
You almost got it:
def main():
signal = []
i = 0
print("Enter the data points of the signal. Stop with empty line.\n")
while True:
i = 1
data = input("Data point {}: ".format(i))
if data == "":
break
signal.append(float(data))
print("\nThe signal is: {}".format(signal))
main()
CodePudding user response:
You don't need a counter. Just check if the input is empty
signal = []
while True:
points = input("Enter a data point for the signal. Stop with empty line.\n")
if not points:
break
signal.append(float(points))
print(signal)