Use python to do a simple unit conversion program

Hello everyone, in unit conversion, some people make mistakes and confuse the unit conversion from time to time, causing unnecessary troubles.

So what I share with you today is a unit conversion applet made with python's math and time modules (the current unit is not comprehensive, there are only some commonly used units), which includes common unit conversions for length, area, weight, and temperature. This program may have some bugs, and I hope you guys don't like it if you don't like it.

On the source code:

import time
import math
time.sleep(0.5)
print('----------Welcome to the unit conversion system---------')
time.sleep(1)
print()
print( '1. Length conversion')
print('2. Area conversion')
print('3. Weight conversion')
print('4. Temperature conversion')
print()
time.sleep(1)
while True:
    print()
    ask =input('May I ask which unit you want to convert (serial number, press 999 to exit the program):')
    if ask=='1':
        print()
        print('1.cm')
        print('2.decimeter' )
        print('3.meter')
        print('4.km')
        print()
        ask1=input('Please select the first length unit (serial number):')
    
        if ask1=='1':
            print()
            print('You have selected centimeters')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input('Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4= ='2':
                print()
                print(ask3,'cm is equal to',ask3/10,'decimeter')
                time.sleep(1)

            if ask4=='3':
                print()
                print(ask3,'cm is equal to',ask3/100,'meter')
                time.sleep(1)

            if ask4=='4':
                print()
                print(ask3,'cm is equal to',ask3/100000,'meter')
                time.sleep(1)

        if ask1=='2':
            print()
            print('You have chosen decimeter')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input( 'Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='1':
                print()
                print(ask3,'decimeter is equal to',ask3*10,'centimeter')
                time.sleep (1)

            if ask4=='3':
                print()
                print(ask3,'decimeter is equal to',ask3/10,'meter')
                time.sleep(1)

            if ask4=='4':
                print()
                print(ask3,'decimeter is equal to',ask3/10000,'kilometer')
                time.sleep(1)

        if ask1=='3':
            print()
            print('You chose rice')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input(' Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='1':
                print()
                print(ask3,'meter is equal to',ask3*100,'centimeter')
                time.sleep(1 )

            if ask4=='2':
                print()
                print(ask3,'meter is equal to',ask3*10,'decimeter')
                time.sleep(1)

            if ask4=='4':
                print()
                print(ask3,'meter is equal to',ask3/1000,'km')
                time.sleep(1)

        if ask1=='4':
            print()
            print('You have chosen kilometers')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input( 'Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='1':
                print()
                print(ask3,'km is equal to',ask3*100000,'cm')
                time.sleep (1)

            if ask4=='2':
                print()
                print(ask3,'km is equal to',ask3*10000,'decimeter')
                time.sleep(1)

            if ask4=='4':
                print()
                print(ask3,'km is equal to',ask3*1000,'meter')
                time.sleep(1)

    if ask=='2':
        print()
        print('1. Square centimeter')
        print('2. Square decimeter')
        print('3. Square meter')
        print('4. Square kilometer')
        print ()
        ask1=input('Please select the first area unit (serial number)')
    
        if ask1=='1':
            print()
            print('You have selected square centimeters')
            print()
            time.sleep(1)
            ask3 =int(input('Please enter the value of this unit'))
            ask4=input('Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='2':
                print()
                print( ask3,'square centimeter is equal to',ask3/100,'square decimeter')
                time.sleep(1)

            if ask4=='3':
                print()
                print(ask3,'square centimeter is equal to',ask3/10000,'square meter')
                time.sleep(1)

            if ask4=='4':
                print()
                print(ask3,'square centimeter is equal to',ask3/10000000000,'square kilometer')
                time.sleep(1)

        if ask1=='2':
            print()
            print('You have chosen square decimeter')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input ('Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='1':
                print()
                print(ask3,'square decimeter is equal to',ask3*100,'square centimeter')
                time. sleep(1)

            if ask4=='3':
                print()
                print(ask3,'square decimeter is equal to',ask3/100,'square meter')
                time.sleep(1)

            if ask4=='4':
                print()
                print(ask3,'square decimeter is equal to',ask3/100000000,'square kilometer')
                time.sleep(1)

        if ask1=='3':
            print()
            print('You have chosen square meters')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input( 'Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='1':
                print()
                print(ask3,'square meter is equal to',ask3*10000,'square centimeter')
                time. sleep(1)

            if ask4=='2':
                print()
                print(ask3,'square meter is equal to',ask3*100,'square decimeter')
                time.sleep(1)

            if ask4=='4':
                print()
                print(ask3,'square meter is equal to',ask3/1000000,'square kilometer')
                time.sleep(1)

        if ask1=='4':
            print()
            print('You have chosen square kilometers')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input ('Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='1':
                print()
                print(ask3,'square kilometer is equal to',ask3*10000000000,'square centimeter')
                time. sleep(1)

            if ask4=='2':
                print()
                print(ask3,'square kilometer is equal to',ask3*100000000,'square decimeter')
                time.sleep(1)

            if ask4=='4':
                print()
                print(ask3,'square kilometer is equal to',ask3*1000000,'square meter')
                time.sleep(1)


    if ask=='3':
        print()
        print('1.gram')
        print('2.kg')
        print('3.ton')
        print()
        ask1=input('Please select the first weight unit (serial number)')
    
        if ask1=='1':
            print()
            print('You have chosen grams')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input('Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='2':
                print()
                print(ask3,'gram is equal to',ask3/1000,'kilogram')
                time. sleep(1)

            if ask4=='3':
                print()
                print(ask3,'gram is equal to',ask3/1000000,'t')
                time.sleep(1)

            

        if ask1=='2':
            print()
            print('You chose kilogram')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input(' Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='1':
                print()
                print(ask3,'kilogram is equal to',ask3*1000,'gram')
                time.sleep(1 )

            if ask4=='3':
                print()
                print(ask3,'kg is equal to',ask3/1000,'ton')
                time.sleep(1)

            
        if ask1=='3':
            print()
            print('You chose ton')
            print()
            time.sleep(1)
            ask3=int(input('Please enter the value of this unit'))
            ask4=input(' Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='1':
                print()
                print(ask3,'ton is equal to',ask3*1000000,'gram')
                time.sleep(1 )

            if ask4=='2':
                print()
                print(ask3,'t is equal to',ask3*1000,'kg')
                time.sleep(1)

    if ask=='4':
        print()
        print('1. Celsius')
        print('2. Fahrenheit')
        print()
        ask1=input('Please choose a temperature unit (serial number):')

        if ask1=='1':
            print()
            print('You have chosen Celsius')
            print()
            time.sleep(1)
            ask3=float(input('Please enter the value of this unit'))
            ask4=input(' Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='2':
                print()
                c=(ask3+32)*1.8
                print(ask3,'Celsius is equal to',c,'Fahrenheit degrees')
                time.sleep(1)

            
            
        if ask1=='2':
            print()
            print('You have chosen Fahrenheit')
            print()
            time.sleep(1)
            ask3=float(input('Please enter the value of this unit'))
            ask4=input( 'Which unit (serial number) do you want to convert the value of this unit into:')
            if ask4=='1':
                print()
                f=(ask3-32)/1.8
                print(ask3,'Fahrenheit is equal to', f, 'Celsius')
                time.sleep(1)

    if ask=='999':
        print()
        print('Exit the software')
        time.sleep(1)
        break

That's what this article is about, thanks for watching this article, thank you.     
 

Guess you like

Origin blog.csdn.net/hu20100913/article/details/126391522