I have a simple problem: I need to take integer inputs from user and add them to a set. The numbers of integers are unknown, the input process will end when the user input is "Done". Below are my code:
s = set()
print('Please type the number, when you're done please type "Done":')
while True:
try:
a = int(input())
s.add(a)
except:
if a == "Done":
break
else:
print('Integer only, please re-type:')
continue
print(s)
But it didn't work as intended. My idea is simple. If the user types an integer then it's fine, just add to set s. If they type a string (which int(input()
is wrong), then it will go down to except
, if the string is "Done", then break the while True
loop, if it isn't then ask the user to retype and continue the loop.
below are the error if I type a String first and if I type normally:
I try to use a simple if-else statement at the start but it causes traceback right at int(input())
CodePudding user response:
Use the two-argument form of iter
to read the input until Done
is entered. You can iterate over the resulting iterator, handling integer conversions and updating the set in the body of a for
loop.
s = set()
def get_input():
return input('''Please type the number, when you're done please type "Done": ''')
for x in iter(get_input, "Done"):
# x is guaranteed to *not* be Done here, or the loop
# would have already exited.
try:
a = int(x)
except ValueError:
print("Integer only, please re-type")
continue
s.add(a)
If you weren't concerned about error handling, you could reduce this to a single set comprehension:
s = {int(x) for x in iter(get_input, "Done")}
CodePudding user response:
When the exception happens, a
remains unset. Try this instead.
s = set()
# Notice also correct quoting
print("Please type the number, when you're done please type 'Done':")
while True:
a = input()
try:
n = int(a)
s.add(n)
# Avoid blanket except
except ValueError:
if a == "Done":
break
else:
print('Integer only, please re-type:')
continue
print(s)
CodePudding user response:
take the input as string first and then convert it to integer before adding to the set. This way you can check if the value entered by the user is "Done" and exit the loop if it is. Something like below (please also check some syntax errors that you have in your code.
s = {}
print('Please type the number, when you\'re done please type "Done":')
while True:
a = input()
if a == 'Done':
break
else:
try:
s.add(int(a))
except:
print('Integer only, please re-type:')
print(s)
CodePudding user response:
s = set()
b = True
print("Please type the number, when you're done please type 'Done'.")
while b:
try:
a = input("Input: ")
if a == "Done":
b = False
break
a = int(a)
s.add(a)
except ValueError:
print("Whoops, please type an integer!")
print(s)
CodePudding user response:
You can try the code below.
s = set()
print('Please type the number, when you\'re done please type "Done":')
while True:
try:
a = input()
if a == "Done":
break
s.add(int(a))
except:
if a == "Done":
break
else:
print('Integer only, please re-type:')
continue
print(s)