Reputation: 89
# Let's create a file and write it to disk.
filename = "test.dat"
# Let's create some data:
done = 0
namelist = []
while not done:
name = raw_input("Enter a name:")
if type(name) == type(""):
namelist.append(name)
else:
break
For the Python code above, I tried, but could not break from the while loop. It always asks me to "Enter a name:", whatever I input. How to break from the loop?
Upvotes: 0
Views: 2749
Reputation: 8506
This is because raw_input
always returns a string, i.e., type(name) == type("")
is always true. Try:
while True:
name = raw_input("Enter a name: ")
if not name:
break
...
Upvotes: 1
Reputation: 59604
# Let's create a file and write it to disk.
filename = "test.dat"
# Let's create some data:
namelist = []
while True:
name = raw_input("Enter a name:")
if name:
namelist.append(name)
else:
break
This breaks when entered nothing
Upvotes: 4