Need help with this script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Loismustdie129
    New Member
    • Aug 2006
    • 194

    Need help with this script

    What I basically want to do is call two numbers x and y. I want to make a loop so that (x = x+1 and y=y+10) and then wait 5 seconds. Here is the script I have been toying with.

    Code:
    from time import *
    
    def byte():
        x = input("what is your x value? ")
        y = input("what is your y value? ")
        if y <= x:
            while y < 1000:
                z = y/x
                return z
            x = x + 1
            y = y + 10
            time.sleep(5)
    I think I have a problem with my indent but every where I check it says I am right. Anyone know what I am doing so wrong here
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Loismustdie129
    What I basically want to do is call two numbers x and y. I want to make a loop so that (x = x+1 and y=y+10) and then wait 5 seconds. Here is the script I have been toying with.

    Code:
    from time import *
    
    def byte():
        x = input("what is your x value? ")
        y = input("what is your y value? ")
        if y <= x:
            while y < 1000:
                z = y/x
                return z
            x = x + 1
            y = y + 10
            time.sleep(5)
    I think I have a problem with my indent but every where I check it says I am right. Anyone know what I am doing so wrong here
    When the code execution reaches 'return z', function byte() returns value 'z' to the calling code. Input x=4, y=2, y<=x is True, y< 100 is true, z=y/x = 0 (integer math), byte() returns 0.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by Loismustdie129
      What I basically want to do is call two numbers x and y. I want to make a loop so that (x = x+1 and y=y+10) and then wait 5 seconds. Here is the script I have been toying with.

      Code:
      from time import *
      
      def byte():
          x = input("what is your x value? ")
          y = input("what is your y value? ")
          if y <= x:
              while y < 1000:
                  z = y/x
                  return z
              x = x + 1
              y = y + 10
              time.sleep(5)
      I think I have a problem with my indent but every where I check it says I am right. Anyone know what I am doing so wrong here
      What BV said about integer division is fixed with y/float(x).
      Did you put the sleep in there as a test to see if it was reach that piont? print "got here" works much better. You need to put the increment of y inside the while loop:
      Code:
      from time import *
      
      def byte():
          x = input("what is your x value? ")
          y = input("what is your y value? ")
          z = "while loop never ran"
          if y <= x:
              while y < 1000:
                  z = y/x
                  x = x + 1
                  y = y + 10
          return z    # nothing below return will be executed
              time.sleep(5)
      
      answer = byte()
      print answer

      Comment

      • Loismustdie129
        New Member
        • Aug 2006
        • 194

        #4
        Originally posted by bartonc
        What BV said about integer division is fixed with y/float(x).
        Did you put the sleep in there as a test to see if it was reach that piont? print "got here" works much better. You need to put the increment of y inside the while loop:
        Code:
        from time import *
        
        def byte():
            x = input("what is your x value? ")
            y = input("what is your y value? ")
            z = "while loop never ran"
            if y <= x:
                while y < 1000:
                    z = y/x
                    x = x + 1
                    y = y + 10
            return z    # nothing below return will be executed
                time.sleep(5)
        
        answer = byte()
        print answer
        I actually included sleep in order to hold the answer to be able to take it down. I could avoid this if there was a way to write this into a text file automatically. You guys got anything for that?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by Loismustdie129
          I actually included sleep in order to hold the answer to be able to take it down. I could avoid this if there was a way to write this into a text file automatically. You guys got anything for that?
          Something like this:
          Code:
          def byte(fn):
              f = open(fn, 'w')
              x = input("what is your x value? ")
              y = input("what is your y value? ")
              if y <= x:
                  while y < 1000:
                      x = x + 1
                      y = y + 10
                      f.write('x = %s and y = %s' % (x, y))
              f.close()
              return True
          
          fn = 'your_file_name'
          byte(fn)
          I tried it using x=750 and y=21. The last few lines from the file:
          x = 845 and y = 921
          x = 846 and y = 931
          x = 847 and y = 941
          x = 848 and y = 951
          x = 849 and y = 961
          x = 850 and y = 971
          x = 851 and y = 981
          x = 852 and y = 991
          x = 853 and y = 1001

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by Loismustdie129
            I actually included sleep in order to hold the answer to be able to take it down. .... You guys got anything for that?
            Yeah. Run it in an IDE instead of the command line. That way you can print to the screen and it stays until you close the IDE.

            Comment

            Working...