事先警告,我刚刚开始学习Python,这是我第一次在此站点上学习。如果我像一个n00b,请不要讨厌。
因此,我创建了一个程序,该程序应该告诉您以光速到达恒星(指定距离)需要多长时间,以及光速的因素。它以一个名为easygui的库开始,该库创建了一个漂亮的窗口,用户可以选择一个因子。他们选择的因素成为变量“选择”。代码的这一部分工作正常。理想情况下,该值随后将被馈送到一个函数中,该函数将进行分解,并返回旅行天数的值。这是不成功的。最有可能的是,我只是将错误设置为错误,因此,如果有人知道使用函数的正确方法,我将非常感谢您的帮助!哦,我试着像疯了似的评论,所以希望一切都说得通!
import easygui as eg #the gui creation library I am using
dist = 41000000000000 #distance to the star
light = 300000 #speed of light
def Convert (factor): #takes in factor chosen by user
speed = light*factor #the speed is the factor multiplied by the speed of light
time = (dist/speed)/3600 # the time is the distance/divided by the speed, since thats a huge value in seconds, the /3600 should reduce it to days
return time #"should" return the value it got for "time"
msg = "Choose a warp factor:" #creates a gui window for user to select factor
title = "Warp Factor Selection"
choices = ["1", "3", "5", "10", "50", "100", "200", "500", "1000"]
choice = eg.buttonbox(msg, title, choices) #gui returns the user's selection as "choice" WORKS!
choice = float(choice) #changes choice to float
if choice == 1:
Convert(choice) #attempts to feed "choice" into the function "convert" DOES NOT WORK :(
print (Convert(1)) #then print the value created from convert (have also tried print(time) but it always returns 0)
此时,故意将其设置为仅接受选择1作为因子。我想先了解一下此功能,然后再做其他可能的因素
当你做
(dist/speed)/3600
如果(dist/speed)
小于3600,则结果将为0。您可以自己尝试一下,
print 3599/3600
将打印
0
因此,您需要像这样将数据转换为浮点型
def Convert (factor):
speed = light*factor
return (float(dist)/float(speed))/3600.0
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句