Python循環實現位置交換


 

四小花旦:楊冪,鄭爽,楊紫,趙麗穎,編寫程序交互第一的寶座,實現結果如下:

['鄭爽', '楊紫', '趙麗穎', '楊冪']
['楊紫', '趙麗穎', '楊冪', '鄭爽']
['趙麗穎', '楊冪', '鄭爽', '楊紫']
['楊冪', '鄭爽', '楊紫', '趙麗穎']

 

star= ['楊冪','鄭爽','楊紫','趙麗穎']
for i in range(4):
    star1 = star.pop(0)  # 運用pop()函數,同時完成提取和刪除。
    star.append(star1)  # 將移除的student1安排到最后一個座位。
    print(star)

#用while循環
students = ['楊冪','鄭爽','楊紫','趙麗穎']
i=0
while i<4:
    i+= 1
    student1=students.pop(0) #移除第一個
    students.append(student1)
    print(students)

  

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM