• Python超入门(4)__迅速上手操作掌握Python


     

     

    # 15.while循环
    1. rows = 0
    2. while rows < 5:
    3. print('*' * rows)
    4. rows += 1
    5. '''
    6. *
    7. **
    8. ***
    9. ****
    10. '''

     

    # 16.使用while循环制作猜灯谜游戏
    
    1. secret_num = 12
    2. guess_count = 0
    3. guess_limit = 100
    4. while guess_count < guess_limit:
    5. guess_count = int(input("猜测:"))
    6. if guess_count == secret_num:
    7. print("你赢了!")
    8. break
    9. else:
    10. print("很抱歉,你失败了!")
    11. '''
    12. 猜测:10
    13. 很抱歉,你失败了!
    14. 猜测:12
    15. 你赢了!
    16. 进程已结束,退出代码为 0
    17. '''

    // break:跳出循环

     

    # 17.打造汽车游戏
    1. command = ""
    2. started = False
    3. print("请输入help查看文档:")
    4. while True:
    5. command = input("> ").lower()
    6. if command == "start":
    7. if started:
    8. print("车辆已在运行中")
    9. else:
    10. started = True
    11. print("车辆开始运行...")
    12. elif command == "stop":
    13. if not started:
    14. print("车辆已经停下了")
    15. else:
    16. started = False
    17. print("车辆停止")
    18. elif command == "help":
    19. print("""
    20. start--车辆开始运行
    21. stop--车辆停止
    22. quit--退出
    23. """)
    24. elif command == "quit":
    25. print("退出系统!")
    26. break
    27. '''
    28. 请输入help查看文档:
    29. > help
    30. start--车辆开始运行
    31. stop--车辆停止
    32. quit--退出
    33. > start
    34. 车辆开始运行...
    35. > start
    36. 车辆已在运行中
    37. > stop
    38. 车辆停止
    39. > stop
    40. 车辆已经停下了
    41. > 1
    42. > quit
    43. 退出系统!
    44. '''

     

    # 18.for循环
    1. # 对字符串进行循环
    2. for item in 'Pyn':
    3. print(item)
    4. '''
    5. P
    6. y
    7. n
    8. '''
    9. # 对字符串数组的循环
    10. for item in ['JoJo', 'Bob', 'Dou']:
    11. print(item)
    12. """
    13. JoJo
    14. Bob
    15. Dou
    16. """
    17. # 对数字进行循环(从0到结尾,不包含本身)
    18. for item in range(3):
    19. print(item)
    20. '''
    21. 0
    22. 1
    23. 2
    24. '''
    25. # 对数字进行循环(从指定的5到8(不包含8))
    26. for item in range(5, 8):
    27. print(item)
    28. '''
    29. 5
    30. 6
    31. 7
    32. '''
    33. # 对数字进行循环(从指定的1到9(不包含9),输出从1 +3递增的数列)
    34. for item in range(1, 9, 3):
    35. print(item)
    36. '''
    37. 1
    38. 4
    39. 7
    40. '''
    练习:输入一个价格数组,将数组内的价格相加。
    1. prices = [12, 23, 56, 21]
    2. prices.sort() # 调用升序排序
    3. print(prices) # [12, 21, 23, 56]
    4. total = 0
    5. for price in prices:
    6. total += price
    7. print(total) # 112

     

    # 19.嵌套循环(在循环内部制造循环),打印九九乘法表
    
    1. # 19.嵌套循环(在循环内部制造循环)
    2. numbers = [2, 3, 9, 4, 1]
    3. for x_count in numbers:
    4. print('x' * x_count)
    5. '''
    6. xx
    7. xxx
    8. xxxxxxxxx
    9. xxxx
    10. x
    11. '''
    12. for x_count in numbers:
    13. output = ''
    14. for count in range(x_count):
    15. output += 'x'
    16. print(output)
    17. '''
    18. xx
    19. xxx
    20. xxxxxxxxx
    21. xxxx
    22. x
    23. '''

    // 练习 :打印九九乘法表

    1. for x in range(1, 10):
    2. for y in range(1, x+1):
    3. print(f'{x}*{y}={x * y}', end='\t')
    4. print()
    5. '''
    6. 1*1=1
    7. 2*1=2 2*2=4
    8. 3*1=3 3*2=6 3*3=9
    9. 4*1=4 4*2=8 4*3=12 4*4=16
    10. 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
    11. 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
    12. 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
    13. 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
    14. 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
    15. '''

    // 注意:Python中可使用,end = " ",取消自动换行,或选择自己需要的结尾控制符号。 

  • 相关阅读:
    Webrtc Android源码编译以及libwebrtc源码抽取
    Docker常见面试题集锦
    深度学习零基础学习之路——第三章 数据可视化TensorBoard和TorchVision的介绍
    金蝶云星空生产管理(冲刺学习)
    【PyTorch】数据封装和模型保存
    帆软报表决策系统 上传excel文件
    常用算法(四)——KMP算法
    Oracle数据库:创建、修改、删除、使用同义词synonym和索引index
    刷题分析工具
    MySQL数据库 主从复制与读写分离
  • 原文地址:https://blog.csdn.net/qq_57233919/article/details/133881313