当前位置:   article > 正文

【简单又有趣】Python五个迷你小项目,即学即用,还不赶紧码住(附源码)_怎么用python做源于日常生活的小发明

怎么用python做源于日常生活的小发明
目录


前言

Python编程语言中,我最喜欢的就是Python的各种第三方库,能够完成很多操作。

下面就给大家介绍5个通过Python构建的项目,以此来学习Python编程。大家也可根据项目的目的及提示,自己构建解决方法,提高编程水平。


一、猜数字游戏

目的:在这个游戏中,任务是创建一个脚本,能够在一个范围内生成一个随机数。如果用户在三次机会中猜对了数字,那么用户赢得游戏,否则用户输。

提示:生成一个随机数,然后使用循环给用户三次猜测机会,根据用户的猜测打印最终的结果。

  1. import random
  2. number = random.randint(1,10)for i in range(0,3):
  3. user = int( input( "guess the number" ))if user number:
  4. print( "Hurray !!")
  5. print( f"you guessed the number right it's {number}")break
  6. elif user>number:
  7. print( "Your guess is too high" )elif user<number:
  8. print( "Your guess is too low." )
  9. print( f"Nice Try !, but the number is {number}")

二、骰子模拟器

目的:创建一个程序来模拟掷骰子。

提示:当用户询问时,使用random模块生成一个1到6之间的数字。

  1. import random;
  2. while int(input( ' Press 1 to roll the dice or 0 to exit:\n')): print(random.randint(1,6))
  3. .------------------------------------------
  4. Press 1 to roll the dice or 0 to exit
  5. 1
  6. 4

三、故事生成器

目的:每次用户运行程序时,都会生成一个随机的故事。

提示:random模块可以用来选择故事的随机部分,内容来自每个列表里。

  1. import random
  2. when = [ 'A few years ago''Yesterday', 'Last night', 'A long time ago' , ' on 20th Jan']
  3. who = [ 'a rabbit ', 'an elephant ', 'a mouse ', 'a turtle ' , 'a cat ']
  4. name = [ 'Ali', 'Miriam' , 'daniel', 'Hoouk ', 'Starwalker']
  5. residence = [ 'Barcelona ' , 'India', 'Germany', 'Venice''England ']
  6. went = [ 'cinema ', 'university' , 'seminar', 'school', 'laundry ']
  7. happened = ['made a lot of friends' , ' Eats a burger', 'found a secret key''solved a mistery','wrote a book ' ]
  8. print( random.choice(when)+ ', ' + random.choice(who) + ' that lived in '+
  9. random.choice(residence) + ', went to the ' + random.choice(went) + ' and ' +
  10. random.choice(happened))
  11. --------------------------------OUTPUT--------------------------.
  12. A long time ago, a cat that lived in England,went to the seminar and solved a mistery

四、自动发送邮件

目的:编写一个Python脚本,可以使用这个脚本发送电子邮件。

提示:email库可用于发送电子邮件。

  1. import smtplib
  2. from email.message import EmailMessage
  3. email = EmailMessage() ## Creating a object for EmailMessage
  4. email['from'] = 'xyz name' ## Person who is sending
  5. email['to'] = 'xyz id' ## Whom we are sending
  6. email['subject'] = 'xyz subject' ## Subject of email
  7. email.set_content("Xyz content of email") ## content of email
  8. with smtlib.SMTP(host='smtp.gmail.com',port=587)as smtp:
  9. ## sending request to server
  10. smtp.ehlo() ## server object
  11. smtp.starttls() ## used to send data between server and client
  12. smtp.login("email_id","Password") ## login id and password of gmail
  13. smtp.send_message(email) ## Sending email
  14. print("email send") ## Printing success message

五、Hangman

目的:创建一个简单的命令行hangman游戏。

提示:创建一个密码词的列表并随机选择一个单词。现在将每个单词用下划线“_”表示,给用户提供猜单词的机会,如果用户猜对了单词,则将“_”用单词替换

  1. import time
  2. import random
  3. name = input("What is your name? ")
  4. print ("Hello, " + name, "Time to play hangman!")
  5. time.sleep(1)
  6. print ("Start guessing...\n")
  7. time.sleep(0.5)
  8. ## A List Of Secret Words
  9. words = ['python','programming','treasure','creative','medium','horror']
  10. word = random.choice(words)
  11. guesses = ''
  12. turns = 5
  13. while turns > 0:
  14. failed = 0
  15. for char in word:
  16. if char in guesses:
  17. print (char,end="")
  18. else:
  19. print ("_",end=""),
  20. failed += 1
  21. if failed == 0:
  22. print ("\nYou won")
  23. break
  24. guess = input("\nguess a character:")
  25. guesses += guess
  26. if guess not in word:
  27. turns -= 1
  28. print("\nWrong")
  29. print("\nYou have", + turns, 'more guesses')
  30. if turns == 0:
  31. print ("\nYou Lose")


总结

以上就是今天分享的内容,针对上面这些项目,有些项目大家可以根据自己的需求进行适当调整。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/337286
推荐阅读
相关标签
  

闽ICP备14008679号