I found the following code example online, which exits a while True loop by using a specific integer:
while True:
i = int(input('Please enter an integer (or 0 to exit):\n'))
if i == 0:
print("Exiting the Program")
break
print(f'{i} square is {i ** 2}')
I would like to know how to exit the loop using a str input (like "exit"), instead of a specific int (0 in this example).
I messed around by creating one input per type:
while True:
i_str = input('Please enter an integer ( or "exit" to exit): ')
if i_str == "exit":
print("Exiting the Program")
break
i_int = int(input('Please enter an integer (or "exit" to exit): '))
print(f'{i_int} square is {i_int ** 2}')
...but it prints out smth every other try, and throws a ValueError when computing 'exit' on the wrong try.
I basically can't figure out how to implement this properly, neither am I sure that I should even be using two different input formats to solve this.