jeudi 11 octobre 2018

Use logic to exit loop (instead of sys.exit, quit, assert, etc)

Beginner here... Python 101

I have a list of if-else statements and need them to stop if the user enters a number outside a predefined range. But I can't use the quit/assert/sys.exit command to stop the program. What is the 'logic' way to exit a command?

Ex: how would i use logic to exit this if statement (4 other separate statements follow it)?

    input_miles = float(input('Enter miles here: '))
    if input_miles >= 0:
        miles2km = input_miles*1.6
        print(input_miles, 'miles equal to', miles2km, 'km.')
    elif input_miles <0:
        assert FALSE

I am trying to check this box... "If the user enters an invalid value, then the program will issue an error message and terminate immediately."

I know it's a basic question, but your help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire