Last answered:

03 May 2022

Posted on:

02 Mar 2021

0

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))

8 answers ( 1 marked as helpful)
Instructor
Posted on:

10 Mar 2021

0

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

Posted on:

11 Mar 2021

1

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)

image.png

Posted on:

16 Mar 2021

1

Looks like there's a prob with solution for Question 5: Screen Shot 2021-03-16 at 14.46.54.png

Instructor
Posted on:

22 Mar 2021

0

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.imageimage
Hope this has illuminated the issue. I believe this would aid Edwin as well!
A., The 365 Team.

Posted on:

02 Apr 2021

0

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?

Posted on:

30 May 2021

0

just to add up another way of solving it image

Posted on:

23 Oct 2021

0

Hello,
Just another to solve it without using nested loop.image

Posted on:

03 May 2022

0

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():
          ^




Submit an answer