2

ユーザー入力を float の形式で受け取り、その数値を MM からインチに変換するプログラムを作成しました。私は最近、プログラムを実行すると、プログラムがループする (ユーザー入力を受け取る) ほど、プログラムが使い始めるメモリが増えることに気付きました。ユーザー入力を受け取るたびにメモリを使用しないようにするにはどうすればよいですか? 私はプログラミングに非常に慣れていないことを付け加えておきます.Pythonは私が独学で学び始めた最初の言語です.Pythonドキュメント以外の本は使用していません.ほとんどの場合、他の人のソースコードを調べて実行/編集しています.何が何をしているのかを把握するために。これは私が自分で書いた最初のプログラムです。そして、私はPythonを独学で3週間弱しか教えていません。コードは次のとおりです

Inches = float(25.4)
Surface_Inches = "**********Surface in Inches**********"
Enter_Number = "**********Enter Number in MM**********"
Final_Number = "**********Number in Inches**********"
WTF = "Sorry I could not recognize that, please try again"
keep_running = True
help = """
        Commands:
        type (exit, quit, end, or kill) to exit the program
        type RA to enter Surface finish conversion
        If you are in Surface conversion mode
        you must enter Ra number in MM that
        needs converted to Inches.
      """

import time
import math
import os

print("Welcome to the converter")
time.sleep(.75)
print("I can convert units of measurement from MM to Inches")
time.sleep(.75)

while keep_running:
    print("Enter Number or type RA for Surface conversion")
    print(Enter_Number)
    number = (input())
    if number in('end', 'quit', 'exit', 'kill'):
        break
    elif number in('clear'):
        os.system('CLS')
    elif number in('help'):
        print(help)
    elif number in('Ra', 'RA', 'ra', 'rA', 'Surface', 'Finish'):
        print("Enter the Surface Finish in MM")
        Surface = (input())
        if Surface in('help'):
            print(" ")
            print(help)
            print(" ")
        else:
            try:
                Surface_Finish = float(Surface)
            except ValueError:
                print(WTF)
            else:
                Surface_IN_Finish = Surface_Finish/Inches
                Answer = (float(Surface_IN_Finish *1000))
                if Answer < 1 : #if the converted results is less than 1 the answers 1
                    Answer = 1
                print(Surface_Inches)
                print(" ")
                print('\t', "        %.0f" % Answer)
                print(" ")
                print(Surface_Inches)
else:
    try:
        MM = float(number)
    except ValueError: #checks to see if the user input is a number
        time.sleep(0.5)
        print(WTF)
    else:
        Results = float(MM \ Inches)
        Final_Form = ("%.3f%" % Results) #creates a float that goes to 3 decimal places
        print(Final_Number)
        print(" ")
        print('\t', "        ", Final_form)
        print(" ")
        print(Final_Number)
        from tkinter import Tk #Imports the Tk Module
        final_form2 = (str(final_form))
        r = Tk()
        r.withdraw() # I have no idea what this does
        r.clipboard_clear()
        r.clipboard_append(final_form2) #appends final_form2 to clipboard 
4

0 に答える 0