广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python while循环
  • 442
分享到

python while循环

python 2023-01-31 07:01:11 442人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

输出1到100之间的所有奇数和偶数:   num = 1    while num <=100:        if num%2 == 0:     print(num)    num += 1 cai    num = 1

输出1到100之间的所有奇数和偶数:

   num = 1

   while num <=100:
       if num%2 == 0:
	    print(num)
   num += 1
	cai
   num = 1

   while num <=100:
       if num%2 == 1:
	    print(num)
   num += 1


猜年龄游戏的两种方法:

age = 50

flag = True

while flag:
    user_input_age = int(input("Age is:"))
    if user_input_age == age:
        print("Yes")
        flag = False
    elif user_input_age > age:
        print("is bigger")
    else:
        print("is smaller")
print("End")

age = 50

while True:
    user_input_age = int(input("Age is:"))
    if user_input_age == age:
        print("Yes")
        break
    elif user_input_age > age:
        print("is bigger")
    else:
        print("is smaller")
print("End")


break语句和continue语句。


print中的end语句:

#print("hello world.",end="__")  # \n   \r\n  \r
#print("hello world.",end="__")
#print("hello world.",end="__")

num1 = 0

while num1<=5:
    print(num1,end="_")
    num2 = 0
    while num2<=7:
        print(num2,end="-")
        num2+=1
        
    num1+=1
    print() #  print(end="\n")

#0_0-1-2-3-4-5-6-7-
#1_0-1-2-3-4-5-6-7-


输出#号,指定长高的长方形:

height = int(input("height:"))
width = int(input("width:"))

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

image.png



--结束END--

本文标题: python while循环

本文链接: https://www.lsjlt.com/news/191419.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python - while循环
    for 循环用在有次数的循环上。while循环用在有条件的循环上。while循环,知道表达式为假,才退出。while循环,表达式是一个逻辑表达式,必须返回一个True或False语法:while expression:    stateme...
    99+
    2023-01-31
    Python
  • python while循环
    输出1到100之间的所有奇数和偶数:   num = 1    while num <=100:        if num%2 == 0:     print(num)    num += 1 cai    num = 1 ...
    99+
    2023-01-31
    python
  • python-for循环与while循环
    格式: while 条件 为 True: 代码块 while True: rayn_age = 18 age = input('请输入你的年龄:') age = int(age) if age == ra...
    99+
    2023-01-31
    python
  • Python循环语句(while循环、for循环)
    Python循环语句 一、while循环二、for语句三、range()函数四、break 和 continue 语句五、pass语句 Python循环语句主要有while循环和for循环...
    99+
    2023-09-04
    python 开发语言 爬虫
  • python的while循环
    while循环#!/usr/bin/python#coding:utf-8i=0sum=0while i<=99:i+=1sum+=iprint sum 先运算再求和 print "总和是:%d"%a总和是:100 ...
    99+
    2023-01-31
    python
  • Python 循环 while,for
    一循环语句(有两种):while 语句for   语句while 语句:问题:输入一个整数n,让程序输出n行的:hello 1hello 2.......hell nwhile 语句:作用:根据一定条件,重复的执行一条语句或多条语句语法:w...
    99+
    2023-01-31
    Python
  • python while循环详解
    1.while循环的基础语法 i = 0while i num: print("你猜的大了") else: print("你猜的小了")pr...
    99+
    2023-09-27
    python 开发语言
  • python While 循环语句
    python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:while 判断条件:     执行语句……执行语句可以是...
    99+
    2023-01-30
    语句 python
  • python while循环实例
    counter = 0 while counter < 3:         print 'loop#%d'  %(counter)         counter+=1 loop #0 loop #1 loop #2...
    99+
    2023-01-31
    实例 python
  • python中的while循环
    1、死循环学会用法 a = 1 while True: print(a) a +=1 2、无限次输入,直到输对,才退出 _age = 18 while True: guess_age = int(input("...
    99+
    2023-01-31
    python
  • Python入门_浅谈for循环、while循环
    Python中有两种循环,分别为:for循环和while循环。 1. for循环 for循环可以用来遍历某一对象(遍历:通俗点说,就是把这个循环中的第一个元素到最后一个元素依次访问一次)。for循环的基本结...
    99+
    2022-06-04
    浅谈 入门 Python
  • python 3 while 循环示例
    示例一: AGE = 20 count = 0 while True:     if count == 5:         break     GUESS = int(input("AGE:"))     if GUESS == AGE...
    99+
    2023-01-31
    示例 python
  • Python基础:for、while循环
    一、While循环 条件控制循环,while后面的condition是真,执行代码块;假,退出循环。可以使用break,强制退出循环。使用else,运行while正常结束时执行的代码块。(break和return退出不执行else)使用co...
    99+
    2023-01-31
    基础 Python
  • 《Python入门到精通》循环语句 while循环,for循环
    「作者主页」:士别三日wyx 「作者简介」:CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「推荐专栏」:小白零基础《Python入门到精通》 循环语句 ...
    99+
    2023-09-04
    python 机器学习 人工智能
  • Python的while循环和for循环如何使用
    本文小编为大家详细介绍“Python的while循环和for循环如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python的while循环和for循环如何使用”文章能帮助大家解决疑惑,下面跟着小编...
    99+
    2022-10-19
  • python怎么跳出while循环
    要跳出while循环,可以使用break语句。一旦执行到break语句,程序会立即跳出循环,继续执行循环后的代码。例如,下面的示例中...
    99+
    2023-10-11
    python
  • Python教程: while循环20例
    Python教程: while循环20例 介绍 循环是计算机编程中最常用的结构之一。在Python中,有两种类型的循环:while循环和for循环。在本文中,我们将专注于while循环并提供20个实用...
    99+
    2023-09-21
    python 开发语言 while循环
  • Python学习-while循环练习
    1.计算1-100的和 i = 1; total = 0; while i <= 100: total = total + i; i = i + 1; print(total); 2.打印出1-1...
    99+
    2023-01-30
    Python
  • Python学习-while循环语句
    Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。即重复性的做一件事情 语法形式如下: while 判断条件: 条件满足执行语句……   可以通过下面的列子...
    99+
    2023-01-30
    语句 Python
  • Python中的用for,while循环
    使用for循环遍历文件打开文件open     r:以读模式打开    w:以写模式打开    a:以追加模式打开    r+:以读写模式打开    w+:以读写模式打开(参见w)    a+:以读写模式打开(参见a)    rb:以二进制...
    99+
    2023-01-31
    Python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作