Reputation: 155
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: ")
Upvotes: 2
Views: 1515
Reputation: 107287
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: ")
Upvotes: 3
Reputation: 8786
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: ")
Upvotes: 1