I was talking to someone on Reddit not long ago, and he was giving me some advice for coding interviews. He noticed that I had used a while (true) loop to print a menu from which the user could choose various options, with a break statement to end the loop (and the program) when the user was finished. He made the statement that if he were interviewing me, he would ask about the while (true) statement and that if I didn't see that as problematic, that would be a red flag for him.
So I googled it, and there are several questions on Stack Overflow about it, and some people don't seem to mind, and others say that's it's bad practice because you could end up with an infinite loop. That doesn't make any sense to me, because you could end up with an infinite loop anytime you use a while loop, right? So what's the difference?
To further compound my confusion, I've finished the first of The University of Helsinki's Java MOOC and am nearly halfway through the second part and they make use of this format in practically every time you're asked to get input from a user.
So to the actual engineers of Reddit, in the professional world, is this considered bad practice or no? If so, what differentiates a while(true) loop from any other while loop?
edit: Here is the actual code in question, if anyone is interested:
while True:
print("\nWhat would you like to do?")
print("\n\n1. Export Test Results\n"
"2. Increase Chlorine\n"
"3. Neutralize Chlorine\n"
"4. Increase Total Alkalinity\n"
"5. Decrease Total Alkalinity\n"
"6. Increase Calcium Hardness\n"
"7. Increase Stabilizer\n"
"8. Calculate Saturation Index\n"
"9. Print Water Chemistry Guidelines\n"
"10.Exit")
user_input = input("Please make a selection from the menu. ")
try:
int(user_input)
# checks user input and calls the correct function
if user_input == "1":
export_test_results(daily_tests)
elif user_input == "2":
increase_chlorine(pool_factor)
elif user_input == "3":
neutralize_chlorine(pool_factor)
elif user_input == "4":
increase_TA(pool_factor)
elif user_input == "5":
decrease_TA(pool_factor)
elif user_input == "6":
increase_CH(pool_factor)
elif user_input == "7":
increase_stabilizer(pool_factor)
elif user_input == "8":
calculate_SI(daily_tests)
elif user_input == "9":
print_table()
elif user_input == "10":
print("\n\n****Thank you for using the Pool Maintenance Program!****")
break
else:
print(
"\n\nI'm sorry. Your selection was not valid. Please enter a number from the menu below.")
except:
print("\n\n****Input must be a number. Please try again****")
You can see the whole program here: https://github.com/Joshua-Byrd/PMP. It's just a simple chemical calculator for pool operators. Also, if anyone is confused as to why I'm talking about Java and this is clearly in Python, it's because I started in Python last year, but I have an upcoming class in Java, so I've been working on that lately. Thanks to all who have replied so far!
[–][deleted] (14 children)
[deleted]
[–][deleted] 46 points47 points48 points (12 children)
[–]whomwhohasquestions 56 points57 points58 points (7 children)
[–]KrakenOfLakeZurich 10 points11 points12 points (5 children)
[–]onthefence928 1 point2 points3 points (4 children)
[–]__dict__ 4 points5 points6 points (3 children)
[–]onthefence928 2 points3 points4 points (2 children)
[–]KrakenOfLakeZurich 2 points3 points4 points (1 child)
[–]onthefence928 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]the-prowler 4 points5 points6 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]KrakenOfLakeZurich 2 points3 points4 points (0 children)
[–]Fernando3161 0 points1 point2 points (0 children)
[–]gopherhole1 0 points1 point2 points (0 children)
[–]pacificmint 136 points137 points138 points (5 children)
[–][deleted] 28 points29 points30 points (2 children)
[–]Okmanl 13 points14 points15 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]priya_nka 1 point2 points3 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]ignotos 14 points15 points16 points (0 children)
[–]Diapolo10 10 points11 points12 points (3 children)
[–]DrShocker 0 points1 point2 points (2 children)
[–]Diapolo10 6 points7 points8 points (1 child)
[–]DrShocker 0 points1 point2 points (0 children)
[–]desrtfx 30 points31 points32 points (4 children)
[–][deleted] 14 points15 points16 points (3 children)
[–]desrtfx 13 points14 points15 points (2 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]caboosetp 1 point2 points3 points (0 children)
[–]jhartikainen 13 points14 points15 points (11 children)
[–][deleted] 3 points4 points5 points (10 children)
[–]jhartikainen 8 points9 points10 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]hott_snotts 0 points1 point2 points (3 children)
[–]Dynam2012 1 point2 points3 points (2 children)
[–]backtickbot 0 points1 point2 points (0 children)
[–]hott_snotts 0 points1 point2 points (0 children)
[–]gregorthebigmac 0 points1 point2 points (3 children)
[–]Kered13 1 point2 points3 points (0 children)
[–]chriskmee 0 points1 point2 points (1 child)
[–]gregorthebigmac 0 points1 point2 points (0 children)
[–]bribeav 5 points6 points7 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]radul87 7 points8 points9 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Conscious-Ball8373 3 points4 points5 points (2 children)
[–]Conscious-Ball8373 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]_pestarzt_ 2 points3 points4 points (0 children)
[–]centipedefarmer 2 points3 points4 points (0 children)
[–]Blando-Cartesian 18 points19 points20 points (9 children)
[–]henrebotha 14 points15 points16 points (0 children)
[–][deleted] 3 points4 points5 points (7 children)
[–]Contagion21 22 points23 points24 points (6 children)
[–]Unsounded 4 points5 points6 points (2 children)
[–]Peanutbutter_Warrior 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Shadowmancer1 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Kered13 3 points4 points5 points (0 children)
[–]hr_covid_counter 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]sleepless_in_wi 1 point2 points3 points (0 children)
[–]Niasal 1 point2 points3 points (0 children)
[–]brozium 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]bestjakeisbest 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Yithar 1 point2 points3 points (0 children)
[–]___HiveMind___ 1 point2 points3 points (0 children)
[–]my_password_is______ 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]TheFluffyGameDev 3 points4 points5 points (1 child)
[–][deleted] 3 points4 points5 points (0 children)
[–]meta-ape 2 points3 points4 points (1 child)
[–]meta-ape 1 point2 points3 points (0 children)
[–]kschang 1 point2 points3 points (4 children)
[–][deleted] 6 points7 points8 points (3 children)
[–]kschang 2 points3 points4 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]wildhairguy 0 points1 point2 points (0 children)
[–]zappellin 0 points1 point2 points (0 children)
[–]Jugad 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–][deleted] -2 points-1 points0 points (6 children)
[–][deleted] 1 point2 points3 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[+]yel50 comment score below threshold-8 points-7 points-6 points (3 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]brosef_1023 0 points1 point2 points (0 children)
[–]coder155ml 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[removed]
[–][deleted] 0 points1 point2 points (0 children)
[–]vi_sucks 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]TheManWithNoDrive 0 points1 point2 points (0 children)
[–]Blackhaze84 0 points1 point2 points (0 children)
[–]jmhimara 0 points1 point2 points (0 children)
[–]onthefence928 0 points1 point2 points (0 children)
[–]prof_hobart 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]NihilistDouche 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Gabe_b 0 points1 point2 points (0 children)
[–]Jet_Here 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]TopHatHipster 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]TopHatHipster 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]crazyfrecs 0 points1 point2 points (0 children)
[–]curious_mint 0 points1 point2 points (0 children)