python-day02 while嵌套循环

while循环
1、输出打印以#组成的长方形,自己定义长和宽。
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
height = int(input("Height:"))
width  = int(input("Width:"))
num_height = 1

while num_height <= height:
    num_width = 1
    while num_width <= width:
        num_width += 1
        print("#",end="")
    num_height += 1
    print()

2、输出如下图形
   *
   * *
   * * *
   * * * *
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = int(input("Width:"))
num_width = 1
while num_width <= width:
    print("#"*num_width,end="\n")
    num_width += 1
3、输出2的倒叙图形:
  * * * *
  * * *
  * *
  *
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = int(input("Width:"))
while width > 0:
    print("#"*width,end="\n")
    width -= 1

第二种实现方式,使用嵌套循环:
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = int(input("Width:"))
while width > 0:
    num_width = width
    while num_width > 0:
        print("*",end="")
        num_width -= 1
    print()
    width -= 1

5、输出99乘法表
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = 1
while width <= 9:
    num_width = 1
    while num_width <= width:
        print(str(num_width)+"*"+str(width)+"="+str(num_width*width),end="\t")
        num_width += 1
    print()
    width += 1
倒叙99表
# -*-encoding:utf-8-*-
‘‘‘
This is script for start docker containor!
Auth: cuishuai
‘‘‘
width  = 9
while width > 0:
    num_width = 1
    while num_width <= width:
        print(str(num_width)+"*"+str(width)+"="+str(num_width*width),end="\t")
        num_width += 1
    print()
    width -= 1

注释:end=表示每一行的结尾,\n表示换行符,\t表示制表符

时间: 2024-08-24 21:21:30

python-day02 while嵌套循环的相关文章

python Day02

交互式输入,判断用户名密码是否匹配 #!/usr/bin/env python# -*- coding:utf-8 -*-# Author Jean import getpass users = "jean"passd = "123456" username = input("username: ")#password = input("password: ")password = getpass.getpass("

My way to Python - Day02

版权声明: 本文中的资料均来自于互联网.将各路内容摘抄于此,作为学习笔记,方便用作后面翻阅查看.如果原作者对文中内容的引用有任何版权方面的问题,请随时联系,我将尽快处理. 特别鸣谢:武沛齐 <Python之路[第二篇]:Python基础(一)> 本节课内容: 基本数据类型 基本数据类型的扩展 作用域: 一个变量只要存在内存里,就可以用.(只要声明了,就可以使用.) # 在后面写函数(面向过程)的时候,需要额外添加一个条件:栈. name = {'nm':123} for item in nam

python练习-程序块-嵌套循环

for multiplier in range(5,8):     for i in range(1,11):         print i ,"x",multiplier,"=",i*multiplier     print # 我尝试将第2个for与第一个for对齐,但是程序就报错了 我的理解,如果拉齐后,就代表第二个程序块成为一个独立的程序块进行运行,这样print计算的时候就无法得到multplier的值,所以就无法完成计算 #result C:\Pyth

Python day02心得

模块初识 os:所有跟系统有关的操作基本都是用此模块 os.system("df -h")      调用系统命令,结果输出打印在屏幕上,返回值为执行结果(0或1) os.mknod(file_name)  创建空文件 os.mkdir(dir_name)    创建文件夹 os.path.exists(path)   判断path是否存在,返回True/False os.path.isfile(path)    判断path是否是一个存在的文件,返回True/False os.pat

初识python(条件判断、循环控制、循环次数限制、常用数据类型、字符串格式化、列表常用操作、二进制运算、嵌套循环)

第一天学习 1.pycharm使用遇到的问题: 如果想运行程序A,一定要右键去执行程序A,而不能直接左下角run,那样的话可能会出现运行之前其他程序 pycharm小技巧: 1.多行全选,shift+tab整体往前缩进一个tab 2.多行全选,tab整体向后缩进一个tab 3.多行全选,ctrl+/注释所选代码,再次按ctrl+/注释取消所选代码 4.pycharm中切换3.5和2.7:file菜单-settings-project pycharmPreject--project interpr

Python 基础语法day02

Python标识符 在python里,标识符有字母.数字.下划线组成. 在python中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. python中的标识符是区分大小写的. 以下划线开头的标识符是有特殊意义的.以单下划线开头(_foo)的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用"from xxx import *"而导入: 以双下划线开头的(__foo)代表类的私有成员:以双下划线开头和结尾的(__foo__)代表python里特殊方法专用的标

Python基础-day02

写在前面 上课第二天,打卡: 大人不华,君子务实. 一.进制相关 - 进制基础 数据存储在磁盘上或者内存中,都是以0.1形式存在的:即是以 二进制 的形式存在: 为了存储和展示,人们陆续扩展了数据的表示形式:八进制 -> 十进制 -> 十六进制... 二进制:0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 ... 八进制:0 1 2 3 4 5 6 7 10 11 12 13 14 15

ParisGabriel:Python无止境 day02

ParisGabriel Python 入门基础 补充: 主流3操作大系统 Windows: Winxp   Win7 Win8 Win10 Unix: Solaris(SUN) IOS(Apple移动端) Mac OS Linux  :(linux基于Unix 独立出来的系统) 安卓(Android) Ubuntu 16.04(当前教学版本) ReadHat CentOS 回顾: Ctrl + Alt + Enter :虚拟机全屏/退出全屏 Ctrl + Alt :释放鼠标 ctrl + sh

day02 - Python基础2

本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码  1. 列表.元组操作                                                                               列表(list)是最常用的数据类型之一,通过列表可以实现对数据的存储.修改等操作. names = ['Tim', 'Jobs', 'Jack'] 通过下标访问列表中的元素,下标从0开始计数. >>> names[0] 'Tim

python学习笔记:Day02

一.列表(list) 1.定义一个列表 name=["tom","jerry","12","13","lose","me"]  2.索引 与字符串的索引一样,列表索引从0开始. print ("第一个元素:%s"%name[0]) >>第一个元素:tom print ("最后一个元素:%s"%name[-1])>>最后一个元