简体   繁体   中英

Python: Parameters to Run the Same Function Multiple Times

I am studying rotating a list, and made a function to rotate the list left and right, but how can I write a code for how many times to rotate? if that makes a sense. I want to pass it as an argument to the functions.

table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]


def rotate_left():
    (table.append(table.pop(0)))
    return table

print(rotate_left())

def rotate_right():
    (table.insert(0,table.pop()))
    return table
    
print(rotate_right())

You can use for loop inside your functions and pass how many times you want to rotate as a argument.

table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]

def rotate_left(rotate_times):
    for _ in range(rotate_times):
        table.append(table.pop(0))
    return table

>>> print(rotate_left(2))
>>> [20, 0, 59, 86, 32, 11, 9, 40, 1, 10]

def rotate_right(rotate_times):
    for _ in range(rotate_times):
        table.insert(0,table.pop())
    return table
    
>>> print(rotate_right(2))
>>> [1, 10, 20, 0, 59, 86, 32, 11, 9, 40]

NOTE

In above scenario, be aware of the fact that, when you pass a list to a method and modify it inside that method, the changes are made in original list unless you make a deep copy , because list is a mutable type.

So, when you call rotate_left(2) , it rotates the original list twice towards left. Then when you call rotate_right(2) , it rotates the original list , which is already rotated by rotate_left(2) , so we got the list as in initial order.

As, the functions are already modifying the original list , you can remove return table from the function (unless you want a new deep copy of list). And simply print the list after that like:

def rotate_left(rotate_times):
    for _ in range(rotate_times):
        table.append(table.pop(0))
 
>>> rotate_left(2)
>>> print(table)
>>> [20, 0, 59, 86, 32, 11, 9, 40, 1, 10]

You can write a 'for loop' and use 'range' to decide how many times you want to rotate. In this example 'rotate_left()' is called three times:

table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]

def rotate_left():
    (table.append(table.pop(0)))
    return table


def rotate_right():
    (table.insert(0,table.pop()))
    return table
    
for i in range(3):
    print(rotate_left())

You can write a ' loop ' and use ' range ' to decide how many times you want to loop. In this example, there is a program that asks the user in which direction and how many times to turn and calculates.

def rotate_left(count,table):
    
    for i in range (count):
        (table.append(table.pop(0)))
    return table

def rotate_right(count,table):
   
    for i in range (count):
        (table.insert(0,table.pop()))
    return table

def main():

    table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]
    isSelect = True

    while(isSelect):
        rotate = int(input("1- Rotate Left\n2- Rotate Right\n: "))
        count = int(input("\nHow many times to rotate ?\n: "))

        if((rotate == 1 or rotate == 2) and count > 0):
            isSelect = False
            if (rotate == 1):
                print(rotate_left(count,table))
                
            elif (rotate == 2):
                print(rotate_right(count,table))
        else:
            print("\nInvalid Parameter. Please choose again.\n")
    
main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM