Basic Python question! I'm asking this question because I'm a beginner!

Asked 1 years ago, Updated 1 years ago, 143 views

How do you make this into Python chords?

I squeezed it like this, but exit keeps getting errors. Help me

python function break

2022-09-22 18:19

2 Answers

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)


2022-09-22 18:19

while True:
    line = input("Enter a Number > ")
    if line == 'exit':
        break
    print('Answer = ', num_mult_3(int(line)))


2022-09-22 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.