61581229 发表于 2018-8-11 13:28:48

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之后,系统判断是字符,不是数字类型了,所以这里就不考虑负数了。
  脚本很简单,下面我吧脚本贴上来,感兴趣的童鞋可以看看:
  


[*]# cat aa.py
[*]#!/usr/bin/env python
[*]print &quot;This script make you input your number \n&quot;
[*]print &quot;Then will show your level...&quot;
[*]def compare(number):
[*]      if number > 100:
[*]                print &quot;Your input is too high&quot;
[*]      elif number >=90 and number <= 100:
[*]                print &quot;Your Level is A&quot;
[*]      elif number >=80 and number < 90:
[*]                print &quot;Your Level is B&quot;
[*]      elif number >=70 and number < 80:
[*]                print &quot;Your Level is C&quot;
[*]      elif number >=60 and number < 70:
[*]                print &quot;Your Level is D&quot;
[*]      elif number < 60:
[*]                print &quot;You not pass&quot;
[*]
[*]
[*]def main():
[*]    while True:
[*]      number=raw_input(&quot;Please input your number:&quot;)
[*]      if number.isdigit():
[*]                Input=int(number)
[*]                print &quot;Your input is &quot;,Input
[*]                compare(Input)
[*]                print &quot;Press Ctrl + C to exit...&quot;
[*]      else:
[*]                print &quot;Please input character ...&quot;
[*]                print &quot;Press Ctrl + C to exit...&quot;
[*]
[*]main()
  

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


[*]# ./aa.py
[*]This script make you input your number
[*]
[*]Then will show your level...
[*]Please input your number:100
[*]Your input is100
[*]Your Level is A
[*]Press Ctrl + C to exit...
[*]Please input your number:99
[*]Your input is99
[*]Your Level is A
[*]Press Ctrl + C to exit...
[*]Please input your number:88
[*]Your input is88
[*]Your Level is B
[*]Press Ctrl + C to exit...
[*]Please input your number:77
[*]Your input is77
[*]Your Level is C
[*]Press Ctrl + C to exit...
[*]Please input your number:66
[*]Your input is66
[*]Your Level is D
[*]Press Ctrl + C to exit...
[*]Please input your number:55
[*]Your input is55
[*]You not pass
[*]Press Ctrl + C to exit...
[*]Please input your number:-100
[*]Please input character ...
[*]Press Ctrl + C to exit...
[*]Please input your number:ijdf
[*]Please input character ...
[*]Press Ctrl + C to exit...
[*]Please input your number:
  
页: [1]
查看完整版本: python比数字游戏