Resolved: Python Programmer Bootcamp- section 6
Hello, I'm having an error with the below code ( int object not callable), and Ican't find the reason:
user_input = input ('please enter a number type exit to stop:>> ')
numbers= []
while user_input.lower() != "exit":
while not user_input.isdigit():
print ('this is not a number! Numbers only please ')
user_input = input('Try again:>> ')
numbers.append(int(user_input))
user_input = input('please add the next number:>> ')
total = 0
for number in numbers:
total += number
print (f'mean is {total/len(numbers)}')
print (sum(numbers)/len(numbers))
Hi Robin!
Great to have you in this course and thanks for reaching out!
I tested your code on my end and it runs without any errors.
Can you please share a screenshot containing the error message? Thank you.
Looking forward to your answer.
Best,
Ivan
I am having a similar problem. Although the numbers are added to the list, it is trapped the second while loop.
user_input = input("Enter a number or type exist to stop: ")
numbers =[]
while user_input.lower() != "exist":
while not user_input.isdigit():
print("Enter Number only!")
user_input = input("Try Again: ")
numbers.append(int(user_input))
user_input = ("Please enter the next number")
print("numbers=",number)
Looks like there's a prob with solution for Question 5:
Hey Nguyen and Edwin,
I've done some tinkering on the problem and managed to replicate your results. There is indeed a subtle weakness in the solution.
When numbers are entered, no problem arises. When you enter a string, the print('That is not a number! Numbers only please:> ') statement fires and afterwards a
new value is received as user_input. However, you need to enter a number in order to escape the nested while-loop. This is a bit unreasonable, because the user
would want to type 'exit' and be done with the program, right? Instead the user is asked for a number...
--------------------------------------------------
In short - if you input a string (not an 'int'), you get stuck in the nested while-loop until an integer has been typed. The only real fixing required is to treat 'exit' as a special string,
which needs to be typed only one time - and the program finishes. Currently 'exit' is treated as all the other strings and that's why we remain in the inner loop.
--------------------------------------------------
FIX: In general, I would suggest restructuring the code -- take a look at the image attached. We can use one while-loop and if-else conditions to navigate our way through various
user inputs. If we check user_input.isdigit() at one place, we can separate the inputted strings. However, since there is only one loop, at each iteration we check whether 'exit' has been typed. If you really insist on using two nested while-loops, then maybe two break-statements are required. However, this is not a very clean solution and I would recommend avoiding it.
Hope this has illuminated the issue. I believe this would aid Edwin as well!
A., The 365 Team.
i have a similar problem and my Spyder is looped in the while.
I would like to update my code and "run" my new code (or stop), but the system has no response.
I have to close and re-open Spyder to fix the problem.
Any better way to solve this symptoms?
just to add up another way of solving it
Hello,
Just another to solve it without using nested loop.
Hi,
got the same prob as you guys. Just use "if not" on the second line ;)
while user_input.lower() != 'exit':
if not user_input.isdigit():
^