Python if、for、while
一、第一个程序
- 交互式输入第一个程序
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 # 定义变量 Google_Mail = "cnblogs.tom@gmail.com" # 允许交互式输入并赋值给对应的变量 Google_Url = input("Please Input Google The Url:") print("邮箱登录地址:%s" %Google_Url) print("您的邮箱:%s" %Google_Mail) print("交互式输入变量值类型:%s"%type(Google_Url)) print("转换后的变量值类型:%s"%type(int(Google_Url))) """ 交互式输入的变量值均是str类型,如需转义需进行强转(数字类型使用int(变量名)) int()转成数字类型、str()转成字符串类型、float()转成浮点数 住:如果变量值本身就是字符串类型强转别的类型是会报错的。 比如:输入的是www.baidu.com 使用int或其他强转到别类型时会报错 """
二、if 语句
- if and
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:40 """ and 当两个条件同时为真时才是真。 交互式输入的变量值均是str类型,如需转义需进行强转(数字类型使用int(变量名)) int()转成数字类型、str()转成字符串类型、float()转成浮点数 """ # 允许交互式输入并赋值给对应的变量 user_name = input("Please input user name:") password = input("Please input user password:") if user_name == "Yuhl" and password == "abc123": print("welcome %s Login successful"%user_name) else: print("username and password errors..")
- if or
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 """ or 其中一个条件为真则为真. 交互式输入的变量值均是str类型,如需转义需进行强转(数字类型使用int(变量名)) int()转成数字类型、str()转成字符串类型、float()转成浮点数 """ # 允许交互式输入并赋值给对应的变量 user_name = input("Please input user name:") password = input("Please input user password:") if user_name == "Yuhl" or password == "abc123": print("welcome %s Login successful"%user_name) else: print("username and password errors..") # 查看变量类型 print("变量[USER_NAME]类型:%s 变量[PASSWORD]类型:%s" %(type(user_name),type(password)))
- if elif
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 """ 交互式输入的变量值均是str类型,如需转义需进行强转(数字类型使用int(变量名)) int()转成数字类型、str()转成字符串类型、float()转成浮点数 """ # 定义变量 my_age = 27 # 允许交互式输入并赋值给对应的变量 user_age = int(input("Please Input your age:")) if user_age == my_age: print("Congratulations, you got it") elif user_age > my_age: print("Oops,got guess bigger") else: print("Oops,got guess small") # 查看变量类型 print("变量[USER_AGE]类型:%s" %type(user_age))
- 格式化输出
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 name = input("name:") age = input("age:") job = input("job:") info=''' -------------info stat------------- Name:%s Age:%s Job:%s -------------info endi------------- '''%(name,age,job) print(info)
三、for 语句
- for 循环打印10个数字
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 #---------------------- 打印10个数字,这里泛指0-9。 如果想打印0-99则将10修改为100 for i in range(10): print(i)
- for 循环流程控制(if)
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 #---------------------- for 循环流程控制(if) for i in range(10): print("------->%s"%i) if i > 7: print("INDEX-INDEX-INDEX:%s"%i)
- for 循环猜年龄游戏(if)【这里这有3次机会】
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 #---------------------- for 循环猜年龄游戏【这里只能猜3次】 Yuhl = 34 for line in range(3): user_age=int(input("Please Input user age:")) if Yuhl == user_age: print("welcome your guessed it") break elif Yuhl < user_age: print("input errors larger. please again input use age") else: print("input errors smaller. please again input use age")
- for else结合使用猜年龄游戏讲解【for else】
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 #---------------------- for else结合使用猜年龄游戏讲解【for else】 Yuhl_age = 27 for line in range(3): user_age = int(input("Please Input user age:")) if user_age == Yuhl_age: print("welcome your guessed it") elif user_age > Yuhl_age: print("input errors larger. please again input use age") else: print("input errors smaller. please again input use age") else: exit("Too Many Attemots....") """ PS:for else的含义是程序在正常执行的时候走else下的代码.如果中间程序有呗break的那么就不会执行else里代码 如果以下代码中的if 判断和break注视加上. 那么会执行else中的代码. 正常执行 如果以下代码中的if 判断和break注视未加. 那么不会执行else中的代码. 这就是区别. 中间截断不执行else下数据 """
- for 循环嵌套使用
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 #---------------------------------------------------------------------------------------- for i in range(10): print(i) if i == 5: break else: print("程序正常结束") #---------------------- for 循环嵌套讲解01 for i in range(5): for j in range(5): print("-------:%s%s"%(i,j)) # 此处pint包含3 if j > 3: break # break跳出整个当前层循环. 只跳一层 print("=======:%s%s"%(i,j)) # 此处pint不包含3. 逻辑问题 #---------------------- for 循环嵌套讲解02 注意:这里break 和continue不能一起使用 for i in range(5): for j in range(5): print("-------:%s%s"%(i,j)) # 此处print的话包含6 if j < 5: break # break. 外层大循环依旧被执行过. 嵌套循环中j < 5就break了. 那么下边的print就不会被执行 # continue # 跳出当前循环. 继续下一次循环 print("=======:%s%s"%(i,j)) # 此处print的话不包含6 逻辑问题
三、while 语句
- while 死循环
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 #---------------------- while 死循环 count = 0 while True: print("welcome your enter while Infinite loop.....%s"%count) count += 1 if count == 22: print("End Of RUN.") break
- while 循环只打印几次
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 #---------------------- while 语句. 只打印多少次 count = 0 while count < 10: print("welcome your enter while Infinite loop.....%s"%count) count += 1
- while 循环流程控制(if)
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 #---------------------- while 语句if进行流控制.只打印某一点数据 count = 0 while True: if count==99: print("welcome your enter while Infinite loop.....%s"%count) break count += 1 #---------------------- while 语句.加if控制流 count = 0 while True: print("welcome your came to Heaven on earth") if count == 5: print("What a paradise on earth it is hell on earth") break count += 1
- while 循环猜年龄游戏
#!/usr/bin/env python # -*- coding: utf-8 -*- #@Author: Tom #@Time: 2024/4/5 22:25 #---------------------- while 猜年龄游戏 count = 0 YHL_age = 27 while count < 3: """strip() #去除空格 isdigit()判断是不是整数 int()转换成整数 """ input_age = input("Please input age:").strip() if input_age.isdigit(): input_age = int(input_age) else: continue if input_age == YHL_age: print("guess correct .....") break elif input_age < YHL_age: print("guess the.") else: print("Guess a little") count += 1
.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~