使用Python判斷學生成績
從 YTYZX有图有真相的百科
此方法可以判斷輸入是否為數字,包含分數為66.5這樣的包含.5的成績。
- -*- coding:UTF-8 -*-
- ============================================================
- Filename pythonlesson1.py
- Version: 1.0
- Author: ytyzx
- Create Date: 2022/11/20
- Update Date: 2022/11/20
- 基礎學習python
- ============================================================
- 定義函數,可以正確分辨小數和負數
def is_number(s):
try: float(s) return True except ValueError: pass try: import unicodedata unicodedata.numeric(s) return True except (TypeError, ValueError): pass return False
inputscore = input('請輸入分數: ')
if is_number(
inputscore
): #isdigit函數只能辨別正整數,對小數和負數的判斷都出現了錯誤。 isnumeric() :是否所有字符均為數值字符,包括Unicode數字、雙字節全角數字、羅馬數字、漢字數字,不包括小數。
stuscore = eval( inputscore ) # eval()会去掉字符串最外层的引号,然后当做Python语句执行[mark:语句or表达式,应该是表达式!],直观理解是:看起来像数字的字符串变成数字了 if stuscore < 0 or stuscore > 100 or stuscore % 0.5 != 0: print('请输入正确的分数') elif 100 >= stuscore >= 80: print('成绩优秀') elif 80 > stuscore >= 60: print('成绩良好') elif 60 > stuscore >= 40: print('成绩及格') else: print('不及格')
else:
print('此处请输入数字')