If you enter the "exit"
string here, val("exit")
will be executed, but you shouldn't assign it to the line
variable. Why don't we just take the input as it is given and process it?
while True:
inputVal = input('Enter a number>')
# Only if the input is a string and its value is exactly "exit"
if inputVal == "exit" :
# Get out of this loop and end it
break
# In all other cases,
else :
# Put the integer of the input value into the function and output something.
print('Answer =', num_mult_3(int(inputVal)))
+ By the way, as described in the problem, it is not necessary to turn to for
to find the value of num_mult_3(n)
. Isn't this actually n 3 3
🤣
import math
def num_mult_3(n) :
# Mathematically, zero is divided by three. If this case is not considered, the last +1 can be subtracted.
return int(math.floor(n/3) + 1)
while True:
line = input("Enter a Number > ")
if line == 'exit':
break
print('Answer = ', num_mult_3(int(line)))
© 2024 OneMinuteCode. All rights reserved.