I wrote some code to get 3 integers from the user. The code then prints the sum of the 3 integers. The code works as expected. I want to validate all input before doing the calculation. However, the code just doesn't feel right. Is there another approach I should be taking? It seems.. redundant. I am looking into nested try/except, but so far no luck.
msg = "Invalid Input"
while True:
try:
a = int(input("Enter 1st number: "))
break
except:
print(msg)
while True:
try:
b = int(input("Enter 2nd number: "))
break
except:
print(msg)
while True:
try:
c = int(input("Enter 3rd number: "))
break
except:
print(msg)
print(a + b + c)
-
1\$\begingroup\$ You could create a function and include on it the while loop and if needed a numerical range (interval) to validate the input so that your code be more cleaner. \$\endgroup\$sɪʒɪhɪŋ βɪstɦa kxɐll– sɪʒɪhɪŋ βɪstɦa kxɐll2020年08月28日 21:21:18 +00:00Commented Aug 28, 2020 at 21:21
-
\$\begingroup\$ @MiguelAvila thanks for the suggestion. I made some edits above. Is that what you were talking about? \$\endgroup\$okkv1747vm– okkv1747vm2020年08月28日 22:55:16 +00:00Commented Aug 28, 2020 at 22:55
-
\$\begingroup\$ Yes, I was referring to that. Btw this video youtube.com/watch?v=C-gEQdGVXbk&t=1781s is really useful, the dude mentions pretty efficient python features to be more expressive with less code. (I would recommend his channel) \$\endgroup\$sɪʒɪhɪŋ βɪstɦa kxɐll– sɪʒɪhɪŋ βɪstɦa kxɐll2020年08月29日 01:49:37 +00:00Commented Aug 29, 2020 at 1:49
-
\$\begingroup\$ Thanks! I will check it out. \$\endgroup\$okkv1747vm– okkv1747vm2020年08月29日 02:18:28 +00:00Commented Aug 29, 2020 at 2:18
-
\$\begingroup\$ Please do not update the code in your question after receiving answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers . \$\endgroup\$Mast– Mast ♦2020年08月29日 06:50:50 +00:00Commented Aug 29, 2020 at 6:50
2 Answers 2
Your revised code in on the right track. You can simplify the function a fair bit:
def get_integer_input(prompt):
while True:
try:
# Just return directly. No need for an invalid_input flag.
return int(input(prompt))
except ValueError:
print('Invalid input')
# Where you see repetition in your code, consider using a data structure to
# address it. There are libraries that will give you the ordinals. Personally,
# I would just rephrase the input() prompt so that the ordinals are not needed.
ORDINALS = ('1st', '2nd', '3rd')
# Get the integers.
fmt = 'Enter {} number: '
a, b, c = [get_integer_input(fmt.format(o)) for o in ORDINALS]
print(a + b + c)
# Or if you care only about the total, just get it directly.
tot = sum(get_integer_input(fmt.format(o)) for o in ORDINALS)
print(tot)
When you have three intimately related variables like this it would be natural to see whether this can be done as a list
instead. Some tips to do that:
- You can use a loop to collect all the items you want.
range(N)
gives you a generator to iterate over numbers 0 through N.- You can either initialize an empty list inside the loop to avoid keeping old results, or continue from the last successfully gathered index to avoid asking the same question twice.
- You can use the inflect library to generate ordinals.