需要有关我现有代码中功能的帮助

时间:2019-06-30 22:04:53

标签: python function

我需要一些有关如何将功能应用于我现有代码的帮助/指导。我的While和for循环可以正常运行,并且代码可以运行,但是我必须使其到达添加函数的位置,并在我的if语句下调用它们。

这是我必须完成的工作---向用户致意此程序并提供菜单选择的功能。此函数将不接受任何参数。 b。将英制/美制单位转换为公制单位的功能。一世。每个函数将以英制/美制单位作为参数ii。它将转换为相应的度量单位。您需要在线找到以下每种转化的转化公式。 iii。确保函数定义中包含return语句。 iv。调用函数时,将函数分配给变量,然后将变量打印给用户。 v。要转换的英制/美制单位。 1.英里到公里2.码到米3.英寸到厘米4.华氏度到摄氏5.磅到千克c。创建一个while循环以询问用户输入(他们希望进行哪种转换),并调用度量转换模块中的函数。

到目前为止,这是我的代码-

print(".....................Metric Conversion........................")

def welcome():
    print("Hello, the purpose of this program is to give the conversion for Imperial/US units.")

def miles(miles):
    return 

def Yards(yards):
    return yards

def Inches(inches):
    return inches

def kilogram(kilogram):
    return kilogram

calculate = 'yes'
while calculate:
    print('''    1) Mile to Kilometers
    2) Yards to Meters
    3) Inches to Centimeters
    4) Fahrenheit to Celsius
    5)Pounds to Kilgrams''')
    answer = input("Please choose and option ")

    if answer == '1':
        miles = float(input("Please enter the Mile to convert to kilometer  "))
        conversion_factor = 1.609
        kilometers = miles * conversion_factor
        print("The conversion from Miles to Kilometers is: ",kilometers)

    elif answer =='2':
        yards = float(input("Please enter the Yard to convert to Meters:  "))
        conversion_rate = 0.9144
        meters = yards * conversion_rate
        print("The conversion from Yards to Meters is: ",meters)

    elif answer =='3':
        inches = float(input("Please enter the Inch to convert to centimeters: "))
        conversion_rate = 2.54
        centimeters = inches * conversion_rate
        print("The conversion from Inches to Centimeters is: ", centimeters)

    elif answer =='4':
        fahrenheit = float(input("Please enter the degree in Fahrenheit to find the Celsius:  "))
        celsius = (fahrenheit -32) *5.0/9.0
        print("The conversion from Fahrenheit to Celsius is: ", celsius)

    elif answer == '5':
        pounds = float(input("Please enter the Pound to convert to Kilogram: "))
        conversion_rate = 0.453592
        kilograms = pounds * conversion_rate
        print("The conversion from Pounds to Kilograms is: ", kilograms)


    else:
        print("You must enter in one of the available options.")

    calculate = input("Would you like to calulate again? ")

1 个答案:

答案 0 :(得分:0)

如果您的目标是减少冗余,您也可以想象使用这样的类:

answer = 1
Converter(*conversions[answer]).run()

然后进行转换:

<div class="section-container">
        <div class="section-box-that-will-grow-on-hover"></div>
        <div class="section-box-that-contains-your-text">
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde iste reiciendis facilis voluptatem ad, odit quia quaerat quis voluptatibus aperiam enim rerum quidem dignissimos, voluptas explicabo, ab fugiat consectetur. Ipsum?</p>
        </div>
    </div>

    <style>
        .section-container{
            color: #fff;
            width: 400px;
            height: 400px;
            position: relative;
            margin-top: 200px;
        }

        .section-box-that-will-grow-on-hover{
            background: #000;
            position: absolute;
            bottom: 0;
            width: 100%;
            height: 400px;
            z-index: -1;
            transition: transform 1s ease;
            transform-origin: bottom;
        }

        .section-container:hover .section-box-that-will-grow-on-hover{
            transform: scale(1, 1.2);
        }

        .section-box-that-contains-your-text{
            color: #fff;
        }
    </style>