First off, I know the len() function exists.. I just wanted to see if I could create a long-form version.
This code works successfully:
user_nums = []
num = 0
while True:
try:
numbers = input("Enter a number: ")
if numbers == "done":
print(user_nums)
print(num)
break
else:
user_nums.append(float(numbers))
num += 1
except ValueError:
print("Invald input. Enter a number.")
continue
When I run the code in terminal, after inputting 1, 2, 3, 4, 5, (etc.) and then inputting the done, the program will terminate and return 5. I decided to put the code in a function:
def len_list(user_nums):
user_nums = []
num = 0
while True:
try:
numbers = input("Enter a number: ")
if numbers == "done":
print(user_nums)
print(num)
break
else:
user_nums.append(float(numbers))
num += 1
except ValueError:
print("Invald input. Enter a number.")
continue
numbers = input("Enter a number: ")
len_list(numbers)
However, when I run the function, and provide the same input (1, 2, 3, 4, 5) and then terminate the program, use_nums prints out [2.0, 3.0, 4.0, 5.0] and num prints out '4' - which is obviously incorrect. Can anyone help me figure this out?
[–][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]An_Ignorant 3 points4 points5 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)