2

Write a program that keeps asking the user for numbers until they enter a non-number.

This is what I have now, it seems I have created an infinite loop.

i = 0
count = 0
while i != (int):
    i = input("Enter a number: ")
1
  • 1
    yes, you need to think of an input you can use to signal your intent to break out of the loop. Also, I think you need to use raw_input Commented Apr 20, 2015 at 13:40

2 Answers 2

3

You can use str.isdigit method ,Note that if you are in python 2 you need to use raw_input because isdigit() is a string method :

i='0'
count = 0
while i.isdigit():
    i = input("Enter a number: ") 

in python 2 :

i='0'
count = 0
while i.isdigit():
    i = raw_input("Enter a number: ")
Sign up to request clarification or add additional context in comments.

2 Comments

Same method as mine, please change this and I will change my vote. Also i is not defined in this sense.
Nothing is wrong now, but at the time of my comment i was not defined and you had a totally different method of doing it at the time of my answer, and then when my answer came along you quick changed your method of solving the question
1

You could ask for a number, and then check if the string entered is a digit using the built in isdigit() method. Currently your code does not ask for a digit to be entered, it just uses 0 automatically. It would not account for a user entering a non-number the very first time.

i = raw_input("Enter a number: ")
while i.isdigit():
    i = raw_input("Enter a number: ")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.