If swap two lines of the code, would it still give the same output?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markos28
    New Member
    • Nov 2008
    • 1

    #1

    If swap two lines of the code, would it still give the same output?

    Here is a mainloop for querying and retrieving stuff.

    def mainloop():
    s = getinput()
    while len(s)>0:
    dosomething(s)
    s = getinput()

    Assume that the getinput function just reads a line of input and puts the contents into s and assume that the dosomething function just prints its string argument.


    What happens if the last two lines of the mainloop function were swapped? Would there be any difference?


    I tried to get the mainloop() working without swapping the two lines first

    def getinput(n):
    s = n

    def dosomething(s):
    print s

    def mainloop():
    s = getinput()
    while len(s)>0:
    s = getinput()
    do_something(s)

    But i seem to get this error:

    TypeError: mainloop() takes no arguments (1 given)


    Some help would be appreciated
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by markos28
    Here is a mainloop for querying and retrieving stuff.
    Code:
    def mainloop():
        s = getinput()
        while len(s)>0:
            dosomething(s)
            s = getinput()
    Assume that the getinput function just reads a line of input and puts the contents into s and assume that the dosomething function just prints its string argument.


    What happens if the last two lines of the mainloop function were swapped? Would there be any difference?


    I tried to get the mainloop() working without swapping the two lines first

    Code:
    def getinput(n):
        s = n
        
    def dosomething(s):
        print s
    
    def mainloop():
        s = getinput()
        while len(s)>0:
            s = getinput()        
            do_something(s)
    But i seem to get this error:

    TypeError: mainloop() takes no arguments (1 given)


    Some help would be appreciated
    Please use code tags when posting code. The error you report does not appear to have anything to do with your code. Function mainloop() takes no arguments, but when you called it, you must have passed an argument.

    If you swapped the last two lines in mainloop(), dosomething() would act upon the second input value of s, which is probably not what you want.

    Comment

    Working...