0

I am doing a multi part lab and its instructions are:

  1. You will define a function named main() (LO 1)

  2. You will get input in the main function and pass it to the following functions: (LO 2)

    a. milesToKm()

    b. FahToCel()

    c. GalToLit()

    d. PoundsToKg()

    e. InchesToCm()

  3. Each function will require that you have a local variable to store the result of the calculation. This result will then be displayed using the print statement from within the function (LO 1, 2, 3)

This is what I have so far:

def main():
    terminate=False
    if terminate == True:
            print('Program has been terminated\n')
    while terminate==False:    
        miles_tries=0
        mile_value=float(input('Roger, Enter a number of miles you wish to convert: '))
        while miles_tries <2 and mile_value<0:
            print('Roger, the value of the miles cannot be a negative number')
            mile_value=float(input('Roger, Enter a number of miles you wish to convert: '))
            miles_tries+=1
        if miles_tries >=2 and mile_value<0:
            terminate=True
        else:
            milesToKm(mile_value)

        temp_tries=0
        temp_value=float(input('Roger, Enter a temperature you wish to convert: '))
        while temp_tries <2 and temp_value>1000:
            print('Roger, the value of the temperature cannot be greater than 1000')
            temp_value=float(input('Roger, Enter a number of degrees fahrenheit you wish to convert: '))
            temp_tries+=1
        if temp_tries >=2 and temp_value>1000:
            terminate=True
        else:
            FahToCel(temp_value)


        gallon_tries=0
        gallon_value=float(input('Roger, Enter a number of gallons you wish to convert: '))
        while gallon_tries <2 and gallon_value<0:
            print('Roger, the value of the gallons cannot be a negative number')
            gallon_value=float(input('Roger, Enter a number of gallons you wish to convert: '))
            gallon_tries+=1
        if gallon_tries >=2 and gallon_value<0:
            terminate=True
        else:
            GalToLit(gallon_value)

        
        pound_tries=0
        pound_value=float(input('Roger, Enter a number of pounds you wish to convert: '))
        while pound_tries <2 and pound_value<0:
            print('Roger, the value of the pounds cannot be a negative number')
            pound_value=float(input('Roger, Enter a number of pounds you wish to convert: '))
            pound_tries+=1
        if pound_tries >=2 and pound_value<0:
            terminate=True
        else:
            PoundToKg(pound_value)

        
        inches_tries=0
        inches_value=float(input('Roger, Enter a number of inches you wish to convert: '))
        while inches_tries <2 and inches_value<0:
            print('Roger, the value of the inches cannot be a negative number')
            inches_value=float(input('Roger, Enter a number of inches you wish to convert: '))
            inches_tries+=1
        if inches_tries >=2 and inches_value<0:
            terminate=True
        else:
            InchesToCm(inches_value)
    
        


    
    
def milesToKm(value):

    km=int(value)*1.6
    print("Roger,", value, " miles is equal to ", km, " kilometers\n")



def FahToCel(value):

    c=(int(value)-32)*5/9
    print("Roger, ",value, " degrees fahrenheit is equal to ", c, " degrees celsius.\n")
        
def GalToLit(value):

    gal=int(value)*3.9
    print("Roger, ",value, " gallons is equal to ", gal, " liters.\n")
       
def PoundToKg(value):

    kg=int(value)*.45
    print("Roger, ",value, " Pounds is equal to ", kg, " kilograms\n")
    
def InchesToCm(value):

    cm=int(value)*2.54
    print("Roger, ",value, " inches is equal to ", cm, " centimeters\n")

main()
print('Have a good day')

I want the entire program to end if the user types in a negative number more than three times for any of the inputs, or if they type a value greater than 100 more than three times for the temperature conversion. It doesn't seem to be working right now. How would I end the whole program LOGICALLY if the user inputs a negative number more than 3 times?

  • Call `sys.exit(status)` to exit with status value `status`. Typically `status` would be `1` for an error, and `0` for successful completion. – Tom Karzes Jun 12 '21 at 01:02
  • If you want to break within the logic, you just need to add a check `if terminate` after each of the two inner while loops – thshea Jun 12 '21 at 01:04
  • @thshea If I do that, the program prints "program terminated" but then asks for the next value to convert. I want the whole program to end. How would I do that ? – Ayman Afeef Jun 12 '21 at 01:08
  • @AymanAfeef I already told you. See my comment. Call `sys.exit` after printing your termination message. Make sure you `import sys`. – Tom Karzes Jun 12 '21 at 01:13
  • 1
    Does this answer your question? [How to terminate a script?](https://stackoverflow.com/questions/73663/how-to-terminate-a-script) – Tom Karzes Jun 12 '21 at 01:16
  • If you don't want to `sys.exit()` as suggested by Tom Karzes then you can also `break` at the same place at which you currently set `terminate=True`. This will break out of the outer loop and thus terminate your program. – Daniel Junglas Jun 12 '21 at 06:05

0 Answers0