Pregunta

Be forewarned, I just started learning Python, and its my first time on this site. If I act like a n00b, please don't hate.

So I have created a program that is supposed to tell you how long it will take you to get to a star (distance is specified) at the speed of light, and factors of the speed of light. It begins with a library called easygui, which creates a nice window, which the user chooses a factor. The factor they have chosen becomes the variable "choice". This section of the code works fine. Ideally, this value would then be fed into a function, which would do the factoring, and return a value for the number of days of travel. This is unsuccessful. Most likely, I have simply set this up wrong, so if anyone knows the proper way to use functions, I'd really appreciate your help! Oh, and I tried to comment like crazy, so hopefully everything makes sense!

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)

At this point in time, it is intentionally set up to only accept the choice of 1 as the factor. I want to figure this function thing out before I go and do the rest of the possible factors

¿Fue útil?

Solución

When you do

(dist/speed)/3600

if the (dist/speed) is lesser than 3600, result will be 0. You can try that out yourself,

print 3599/3600

will print

0

So, you need to convert the data to float like this

def Convert (factor):
    speed = light*factor
    return (float(dist)/float(speed))/3600.0

Otros consejos

thefourtheye already explained why, but if you want to avoid this in the future you could switch to Python 3 division by putting this at the top of your file:

from __future__ import division

In Python 3, it behaves more intuitively in situations like this (1/2 == .5) while you can still get the integer division behavior with // (1//2 == 0)

You may want to do this

if str(choice) in choices:
    Convert(choice)
    print (Convert(choice))

That way, you don't have to make a new if condition to test each number. This just says that if choice is in choices list, execute the function with choice.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top