当前位置:   article > 正文

Python编程案例教程电子版,Python编程案例教程答案_python程序设计案例教程课后答案 徐光侠

python程序设计案例教程课后答案 徐光侠

大家好,小编为大家解答Python编程案例教程 刘庆,姚丽娜,余美华课后答案的问题。很多人还不知道Python编程案例教程 刘庆,姚丽娜,余美华电子版,现在让我们一起来看看吧!

前言

22个通过Python构建的项目,以此来学习Python编程。

① 骰子模拟器

目的:创建一个程序来模拟掷骰子python for语句用法

提示:当用户询问时,使用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 e to exit

② 石头剪刀布游戏

目标:创建一个命令行游戏,游戏者可以在石头、剪刀和布之间进行选择,与计算机PK。如果游戏者赢了,得分就会添加,直到结束游戏时,最终的分数会展示给游戏者。

提示:接收游戏者的选择,并且与计算机的选择进行比较。计算机的选择是从选择列表中随机选取的。如果游戏者获胜,则增加1分。

  1. import random
  2. choices = ["Rock", "Paper", "Scissors"]
  3. computer = random.choice(choices)
  4. player = False
  5. cpu_score = 0
  6. player_score = 0
  7. while True:
  8. player = input("Rock, Paper or Scissors?").capitalize()
  9. # 判断游戏者和电脑的选择
  10. if player == computer:
  11. print("Tie!")
  12. elif player == "Rock":
  13. if computer == "Paper":
  14. print("You lose!", computer, "covers", player)
  15. cpu_score+=1
  16. else:
  17. print("You win!", player, "smashes", computer)
  18. player_score+=1
  19. elif player == "Paper":
  20. if computer == "Scissors":
  21. print("You lose!", computer, "cut", player)
  22. cpu_score+=1
  23. else:
  24. print("You win!", player, "covers", computer)
  25. player_score+=1
  26. elif player == "Scissors":
  27. if computer == "Rock":
  28. print("You lose...", computer, "smashes", player)
  29. cpu_score+=1
  30. else:
  31. print("You win!", player, "cut", computer)
  32. player_score+=1
  33. elif player=='E':
  34. print("Final Scores:")
  35. print(f"CPU:{
  36. cpu_score}")
  37. print(f"Plaer:{
  38. player_score}")
  39. break
  40. else:
  41. print("That's not a valid play. Check your spelling!")
  42. computer = random.choice(choices)

③ 随机密码生成器

目标:创建一个程序,可指定密码长度,生成一串随机密码。

提示:创建一个数字+大写字母+小写字母+特殊字符的字符串。根据设定的密码长度随机生成一串密码。

  1. import random
  2. passlen = int(input( "enter the length of password" ))
  3. s="abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLNNOPQRSTUVWXYZ!@#$%^&*()?
  4. p= "".join(random.sample(s,passlen ))
  5. print (p)
  6. ----------------------------------------------aw--enter the length of password
  7. za1gBo

④ 句子生成器

目的:通过用户提供的输入,来生成随机且唯一的句子。

提示:以用户输入的名词、代词、形容词等作为输入,然后将所有数据添加到句子中,并将其组合返回。

  1. color = input("Enter a color: ")
  2. pluralNoun = input("Enter a plural noun: )celebrity = input(“Enter a celebrity: ")print("Rases are", color)
  3. print(pluralNoun i - are blue-)
  4. print(I love-, celebrity)
  5. -------------------------------------
  6. Red
  7. Teeth
  8. RDJ
  9. Roses are red. teeth are blue.I Love RDJ

⑤ 猜数字游戏

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

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

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

⑥ 故事生成器

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

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

  1. import random
  2. when = [ 'A few years ago'"Yesterday','Last night', 'A long time ago' , ' n 2eth Jan']who = [ 'a rabbit', 'an elephant ', 'a mouse", "a turtie' , 'a cat']
  3. namne =[ 'Ali',_"Miriam " , 'daniel ,"Hoouk ' , 'starwalker"j
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览422343 人正在系统学习中
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/650376
推荐阅读
相关标签
  

闽ICP备14008679号