python比数字游戏

简介:

 今天看到了一个题目,需要输入一个数字,表示成绩和他的成绩的级别:

A: 90--100

B: 80--89

C: 70--79

D: 60--69

E: < 60

 

    需求在上面大家都看到了,加入输入90-100之间,表示你的级别在A;输入80--89之间,表示你的级别是B;输入的是70--79之间,表示你的级别是C;输入60--69之间,表示你的级别是D;输入小于60,表示你没有通过;

    除了上面的判断之外,我们还需要判断输入的是字符还是数字类型,本来还需要考虑整数和负数的问题,但是由于负数有(负号)-,输入-21之后,系统判断是字符,不是数字类型了,所以这里就不考虑负数了。

     脚本很简单,下面我吧脚本贴上来,感兴趣的童鞋可以看看:

 

 
  1. [root@centos6 20130113]# cat aa.py 
  2. #!/usr/bin/env python 
  3. print "This script make you input your number \n" 
  4. print "Then will show your level..." 
  5. def compare(number): 
  6.         if number > 100
  7.                 print "Your input is too high" 
  8.         elif number >=90 and number <= 100
  9.                 print "Your Level is A" 
  10.         elif number >=80 and number < 90
  11.                 print "Your Level is B" 
  12.         elif number >=70 and number < 80
  13.                 print "Your Level is C" 
  14.         elif number >=60 and number < 70
  15.                 print "Your Level is D" 
  16.         elif number < 60
  17.                 print "You not pass" 
  18.  
  19.  
  20. def main(): 
  21.     while True
  22.         number=raw_input("Please input your number:"
  23.         if number.isdigit(): 
  24.                 Input=int(number) 
  25.                 print "Your input is ",Input 
  26.                 compare(Input) 
  27.                 print "Press Ctrl + C to exit..." 
  28.         else
  29.                 print "Please input character ..." 
  30.                 print "Press Ctrl + C to exit..." 
  31.  
  32. main() 

下面来看看运行的效果吧:

 

 
  1. [root@centos6 20130113]# ./aa.py 
  2. This script make you input your number 
  3.  
  4. Then will show your level... 
  5. Please input your number:100 
  6. Your input is  100 
  7. Your Level is A 
  8. Press Ctrl + C to exit... 
  9. Please input your number:99 
  10. Your input is  99 
  11. Your Level is A 
  12. Press Ctrl + C to exit... 
  13. Please input your number:88 
  14. Your input is  88 
  15. Your Level is B 
  16. Press Ctrl + C to exit... 
  17. Please input your number:77 
  18. Your input is  77 
  19. Your Level is C 
  20. Press Ctrl + C to exit... 
  21. Please input your number:66 
  22. Your input is  66 
  23. Your Level is D 
  24. Press Ctrl + C to exit... 
  25. Please input your number:55 
  26. Your input is  55 
  27. You not pass 
  28. Press Ctrl + C to exit... 
  29. Please input your number:-100 
  30. Please input character ... 
  31. Press Ctrl + C to exit... 
  32. Please input your number:ijdf 
  33. Please input character ... 
  34. Press Ctrl + C to exit... 
  35. Please input your number: 

 本文转自你是路人甲还是霍元甲博客51CTO博客,原文链接http://blog.51cto.com/world77/1117584如需转载请自行联系原作者


world77

相关文章
|
1月前
|
存储 Java C语言
【python】——使用嵌套列表实现游戏角色管理
【python】——使用嵌套列表实现游戏角色管理
31 0
|
9天前
|
存储 Python
如何使用Python实现“猜数字”游戏
本文介绍了使用Python实现“猜数字”游戏的过程。游戏规则是玩家在给定范围内猜一个由计算机随机生成的整数,猜对则获胜。代码中,首先导入random模块生成随机数,然后在循环中获取玩家输入并判断大小,提供猜小、猜大提示。通过增加猜测次数限制、难度选择、优化输入提示和图形化界面等方式可优化游戏。这篇文章旨在帮助初学者通过实际操作学习Python编程。
26 2
|
1月前
|
存储 Python Windows
10分钟学会用python写游戏,实例教程
10分钟学会用python写游戏,实例教程
34 0
|
2月前
|
Python
Python猜字游戏是一种常见的编程练习
Python猜字游戏是一种常见的编程练习
24 2
|
2月前
|
Python
用 Python 写一个猜数字游戏并运行它
用 Python 写一个猜数字游戏并运行它
14 0
|
2月前
|
UED 开发者 Python
制作你的第一个 Python 游戏
想要制作一个 Python 游戏?这是一个令人兴奋的项目!在这篇文章中,我将引导你完成制作第一个 Python 游戏的步骤。即使你没有编程经验,也不用担心,我们将从基础开始,一起探索游戏开发的乐趣。
|
2月前
|
计算机视觉 Python
用 Python 开发简单的游戏
游戏开发是一个充满乐趣和挑战的领域,而 Python 作为一种强大的编程语言,为游戏开发提供了丰富的工具和可能性。在本文中,我们将探讨如何使用 Python 开发简单的游戏,并提供一些基本的示例和指导。
|
2月前
|
存储 编译器 Python
python实战【外星人入侵】游戏并改编为【梅西vsC罗】(球迷整活)——搭建环境、源码、读取最高分及生成可执行的.exe文件
python实战【外星人入侵】游戏并改编为【梅西vsC罗】(球迷整活)——搭建环境、源码、读取最高分及生成可执行的.exe文件
|
2月前
|
IDE 开发工具 Python
用python写出一个猜数字游戏
用python写出一个猜数字游戏
33 4
|
3月前
|
机器学习/深度学习 Python
Python “贪吃蛇”游戏,在不断改进中学习pygame编程
Python “贪吃蛇”游戏,在不断改进中学习pygame编程
54 0
Python “贪吃蛇”游戏,在不断改进中学习pygame编程