python code help (if else statements)

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

python code help (if else statements)

Post by krona64 on Sun Feb 16, 2014 5:01 pm
([msg=79519]see python code help (if else statements)[/msg])

ok so i am having trouble with this and I don't understand how i am suppose to structure my code. I have tried several things, read and reread several things about "if else" statements. I was told I need to place my statements first to identify which conversion will be done the use a function to do the conversion. I am really lost and everything I have tried has not given me the results I need.

what I need it to do, is to terminate the program if the user enters an invalid input.
what the program does now, is it prompts the user that the input is invalid but continues to the next function.

Code: Select all

# lab assignment 4



# standard to metric conversions


name = input ('What is your name? ')

# define limitations on input values for functions

min_mile = 0

min_gal = 0

min_pound = 0

max_fah = 1000

min_inch = 0

# define main function

def main():

    print('hello',name,', today we will be doing some standard to metric conversions.')

    print('The first conversion will be to convert miles to kilometers.')

    MileToKm()

    print ('The next conversion we will do, will be to convert Fahrenhiet to Celsius.')

    FahToCel()

    print ('The next conversion will be to convert gallons to liters.')

    GalToLit()

    print('The next conversion will be to convert pounds to kilograms.')
   
    PoundsToKg()

    print ('The last conversion will be to convert inches to cenitmeters.')

    InchToCm()

    print ('That concludes this segment of standard to metric conversions. Till next time.')

# ************************************************* end of main function ******************************************

# definning functions

# define mile to kilometer function


def MileToKm():

    mile = float(input('What is the number of miles that you would like to convert? '))
       
    mile_conv = mile * 1.6

    if mile_conv > min_mile :

        print ('The result would be', format(mile_conv,'.2f'),'kilometers.')
   
    else:

        print ("Sorry that is an invalid input.")

   

# define fahrenhiet to celsius function

def FahToCel():

        temp = float(input('What degrees in Fahrenhiet would to like to convert to Celsius? '))

        temp_conv = (temp - 32) * 5/9

        if temp_conv < max_fah :

            print('The result would be',format(temp_conv,'.1f'),'degrees Celsius.')

        else :
           
            print ("Sorry that is an invalid input.")
# define gallon to liter function

def GalToLit():

        gallon = float(input('What is the amount of gallons that you would like to convert to liters? '))

        gallon_conv = gallon * 3.9

        if gallon_conv > min_gal :

            print ('The result would be',format(gallon_conv,'.1f'),'liters.')

        else :

            print ("Sorry that is an invalid input.")

# define pounds to kilograms function

def PoundsToKg():

        pounds = float(input('What is the amount of pounds that you would like to convert to kilograms? '))

        pound_conv = pounds * 0.45

        if pound_conv > min_pound :

            print('The result would be',format(pound_conv,'.2f'),'kilograms.')

        else :

            print ("Sorry that is an invalid input.")
       
# define inch to centimeter function

def InchToCm():

        inches = float(input('how many inches would you like to convert to centimeters? '))

        inch_conv = inches * 2.54

        if inch_conv > min_inch :

            print('the result would be',format(inch_conv,'.2f'),'centimeters.')

        else :

            print ("Sorry that is an invalid input.")


           
#*************************************end of functions defined********************************************



# call for main function to run

main()





I am lost and I dont know how it should be structured. I dont have a solid example to look at to compare and see exactly what it is I am doing wrong.

any input would be greatly appreciated.

-- Sun Feb 16, 2014 5:35 pm --

I found a work around to use and that is

import sys
sys.exit("input invalid")

while that kills the program. That is technically not how the teacher wanted us to do it. we have not learned that syntax yet.
so I still am i little lost.
Last edited by krona64 on Sun Feb 16, 2014 5:37 pm, edited 1 time in total.
krona64
New User
New User
 
Posts: 19
Joined: Thu Mar 21, 2013 12:20 am
Blog: View Blog (0)


Re: python code help (if else statements)

Post by Tentra on Sun Feb 16, 2014 5:35 pm
([msg=79520]see Re: python code help (if else statements)[/msg])

I don't really understand why you're having trouble. Your code works fine, you just need to add another statement in your else clauses, it has nothing to do with structure. I don't use Python so I may be straying from typical coding convention, but I believe this is what you're trying to achieve:

Code: Select all
from sys import exit

...

def MileToKm():
    mile = float(input('What is the number of miles that you would like to convert? '))
    mile_conv = mile * 1.6
    if mile_conv > min_mile :
        print ('The result would be', format(mile_conv,'.2f'),'kilometers.')
    else:
        print ("Sorry that is an invalid input.")
        exit()


Notice the 'from sys import exit' and the 'exit()' in your else.

-- Sun Feb 16, 2014 5:46 pm --

krona64 wrote:-- Sun Feb 16, 2014 5:35 pm --

I found a work around to use and that is

import sys
sys.exit("input invalid")

while that kills the program. That is technically not how the teacher wanted us to do it. we have not learned that syntax yet.
so I still am i little lost.


Seems we posted the same solution at the same time! Just a word of advise as you seem to be in an introductory programming class. Don't expect your teacher to give you absolutely everything you need to complete an assignment. I've taken a number of classes where you had to do a good bit of googling to come up with a solution for your problem. It comes with the territory of programming. Unless your teacher explicitly tells you not to do something or to do something in a specific way, do whatever you need to do to accomplish the task.

In your situation, there doesn't seem to be any other way to exit a python program other than sys.exit(), so I believe you'll be fine using it.
User avatar
Tentra
Poster
Poster
 
Posts: 161
Joined: Wed Apr 30, 2008 4:52 pm
Blog: View Blog (0)


Re: python code help (if else statements)

Post by krona64 on Sun Feb 16, 2014 6:39 pm
([msg=79521]see Re: python code help (if else statements)[/msg])

thanks for the response.

I decided i am just going to run with it and if he doesn't like it, well then tough shit.
It doesn't state a process in which how to make it terminate, just that it needs to terminate if
the input is invalid.

thanks again for the response.
krona64
New User
New User
 
Posts: 19
Joined: Thu Mar 21, 2013 12:20 am
Blog: View Blog (0)


Re: python code help (if else statements)

Post by cyberdrain on Mon Feb 17, 2014 5:50 pm
([msg=79528]see Re: python code help (if else statements)[/msg])

Quick question: why wasn't this posted in Programming?
Free your mind / Think clearly
User avatar
cyberdrain
Contributor
Contributor
 
Posts: 982
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests