1.打印此号码:
1 2 3 4 5 5 4 3 2 1
no = 1 top = 5 direction = 1 while no>0: print(no,end= ' ') if no == top: print(no,end=' ') direction = -1 no = no + direction
1 2 3 4 5 5 4 3 2 1
2.猜数字游戏:
import random system_no = random.randint(1,20) while True: guess = int(input("Enter the No. ")) if guess == system_no: print("Hurray! I got the number!! ") break elif guess > system_no: print("Your guess is too high! ") else: print("Your guess is too Low! ")
Enter the No. 15 Your guess is too Low! Enter the No. 17 Your guess is too Low! Enter the No. 19 Hurray! I got the number!!
3.找出数字中最大的数字
no = int(input("Enter no. ")) max_no = 0 while no>0: rem = no%10 if rem>max_no: max_no = rem no//=10 print(max_no)
Enter no. 2357 7
4.找出数字中最小的数字
no = int(input("Enter no. ")) min_no = 9 while no>0: rem = no%10 if rem<min_no: min_no="rem" no print> <pre class="brush:php;toolbar:false">Enter no. 2357 2
5.Collatz 序列
编写一个程序,打印给定数字的 Collatz 序列,直到达到 1。
规则:
If the number is even: n=n/2 If the number is odd: n=3n+1.
def even_odd(no): while no>0: num=no%10 if num%2==0: even=num/2 else: odd=3*num+1 no=no//10 return even,odd no=int(input("Enter the number:")) even,odd=even_odd(no) print("even number:",even) print("odd number:",odd)
Enter the number:12 even number: 1.0 odd number: 4
以上是日 - 循环练习的详细内容。更多信息请关注PHP中文网其他相关文章!

Python脚本在Unix系统上无法运行的原因包括:1)权限不足,使用chmod xyour_script.py赋予执行权限;2)Shebang行错误或缺失,应使用#!/usr/bin/envpython;3)环境变量设置不当,可打印os.environ调试;4)使用错误的Python版本,可在Shebang行或命令行指定版本;5)依赖问题,使用虚拟环境隔离依赖;6)语法错误,使用python-mpy_compileyour_script.py检测。

使用Python数组比列表更适合处理大量数值数据。1)数组更节省内存,2)数组对数值运算更快,3)数组强制类型一致性,4)数组与C语言数组兼容,但在灵活性和便捷性上不如列表。

列表列表更好的forflexibility andmixDatatatypes,何时出色的Sumerical Computitation sand larged数据集。1)不可使用的列表xbilese xibility xibility xibility xibility xibility xibility xibility xibility xibility xibility xibles and comply offrequent elementChanges.2)

numpymanagesmemoryforlargearraysefefticefticefipedlyuseviews,副本和内存模拟文件.1)viewsAllowSinglicingWithOutCopying,直接modifytheoriginalArray.2)copiesCanbecopy canbecreatedwitheDedwithTheceDwithThecevithThece()methodervingdata.3)metservingdata.3)memore memore-mappingfileShessandAstaStaStstbassbassbassbassbassbassbassbassbassbassbb

Listsinpythondonotrequireimportingamodule,helilearraysfomthearraymoduledoneedanimport.1)列表列表,列表,多功能和canholdMixedDatatatepes.2)arraysaremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremeremeremeremericdatabuteffeftlessdatabutlessdatabutlessfiblesible suriplyElsilesteletselementEltecteSemeTemeSemeSemeSemeTypysemeTypysemeTysemeTypysemeTypepe。

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 英文版
推荐:为Win版本,支持代码提示!