我刚刚制作了一个程序,它以浮点数的形式接受用户输入并将数字从 MM 转换为英寸。我最近注意到,当我运行程序时,程序循环越多(接受用户输入),程序开始使用的内存就越多。我怎样才能让它在每次需要用户输入时都不会使用更多的内存?我应该补充一点,我对编程很陌生,python 是我开始自学的第一门语言,除了 Python 文档之外,我没有使用其他书籍,而且我主要只是浏览其他人的源代码并运行它/编辑它弄清楚什么在做什么。这是我自己编写的第一个程序。而且我只自学了不到 3 周的 Python 这是代码
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