2013-07-25 66 views
2

我對編程頗爲陌生,我只是在學習Python。我在下面寫的代碼是一個程序,可以讓你輸入一個等級(例如14/20或70,例如100),然後給你從A到E的等級。基本上我想知道的是如果有辦法「清除」列表中的整數,而不是一個接一個地將它們乘以10,100等等。從Python列表中獲取整數

我總結:我希望成績[「14 sur 20」]成爲a = 14和b = 20,而不必編寫我寫的所有代碼。

PS:我想我的代碼是太長,但我是新來的Python和我沒有足夠的知識還沒有把它縮短,所以不要太硬;)

import os 

grade = input ("Entrez votre note :") 
deter = [] 
redet = [] 

i = z = a = b = x = 0 

while i < len(grade): 
    if grade[i] == "s" and grade[i+1] == "u" and grade [i+2] == "r" : #checking if the grade is written as " x sur y" 
     while z < i-1 : #building a list for the grade 
      deter.append (grade[z]) 
      z += 1 
     z += 5 #jumping to the scale 
     while z < len(grade) : #building a list for the scale 
      redet.append (grade[z]) 
      z += 1 

    elif grade[i] == "/" : #means grade is written as "x/y" 
     while z < i : #building a list for the grade 
      deter.append (grade[z]) 
      z += 1 
     z += 1 #jumping to the scale 
     while z < len(grade) : #building a list for the scale 
      redet.append (grade[z]) 
      z += 1 

    i += 1 

redet = list (map(float, redet)) #converting to integers 
deter = list (map(float, deter)) 

if len(deter)>1 : 
    y = 10**(len(deter)-1) 
else: 
    y = 1 

while x < len(deter) : #making variables 
    a = a + deter[x]*y 
    x += 1 
    y /= 10 

x = 0 
if len(redet)>1 : 
    y = 10**(len(redet)-1) 
else : 
    y = 1 

while x < len(redet) : #making variables 
    b = b + redet[x]*y 
    x += 1 
    y /= 10 

grade = a/b 
if grade >= 0.8 : 
    print("A") 
elif grade >= 0.6 : 
    print("B") 
elif grade >= 0.5 : 
    print("C") 
elif grade >= 0.4 : 
    print("D") 
elif grade <= 0.4 : 
    print("E") 

os.system ("pause") 
+0

如果你瞭解考試是如何分級的,你通過! –

回答

5

您可以使用re.split14 sur 2014/20等字符串拆分爲兩部分。

你可以使用bisect.bisect將分數轉換爲字母等級。

import bisect 
import re 

def lettergrade(score, breakpoints = [40, 50, 60, 80], grades = 'EDCBA'): 
    """ 
    >=80 -> A 
    >=60 -> B 
    >=50 -> C 
    >=40 -> D 
    else -> E 
    """ 
    i = bisect.bisect(breakpoints, score) 
    return grades[i] 


grade = input("Entrez votre note : ") 
a, b = map(int, re.split(r'sur|/', grade)) 
print(lettergrade(100.0*a/b)) 

正則表達式模式的一個解釋是:

`re.split(r'sur|/', grade)` splits the string `grade` into a list of strings. It splits on the regex pattern `r'sur|/'`. This regex pattern matches the literal string `sur` or the forward-slash `/`. The `|` is the regex syntax for "or". 

'sur|/'前面的r是Python語法導致Python來解釋'sur|/'作爲raw string。這會影響反斜槓的解釋方式。該docs for the re module解釋它的用法是這樣的:

正則表達式用反斜槓字符('\')來表示 特殊格式或允許在不調用 其特殊的意義要使用特殊字符。這與Python的 在字符串文字中用於相同目的的用法相沖突;爲 例如,要匹配反斜槓,人們可能不得不寫'\\\\' 作爲圖案串,因爲正則表達式必須是\\,並且每個 反斜槓必須表示爲\\常規Python字符串 字面內部。

解決方案是使用Python的原始字符串表示法來定期使用 表達式模式;在 中不以任何特殊方式處理反斜槓,其字符串文字前綴爲'r'。所以r"\n"是包含'\''n'的雙字符字符串 ,而"\n"是包含換行符的單字符字符串 。通常模式將使用這種原始字符串表示法以Python 代碼表示。

關於原始字符串的完整故事,請參見the language reference doc

雖然在這種情況下,原始字符串r'sur|/'與普通字符串'sur|/'相同,但始終使用原始字符串製作正則表達式模式可能是一種好的做法。在這種情況下它不會受到傷害,並且在其他情況下它肯定會有所幫助。

由於re.split返回一個字符串列表,map(int, ...)用於將字符串轉換成ints

In [37]: grade = '14 sur 20' 

In [38]: re.split(r'sur|/', grade) 
Out[38]: ['14 ', ' 20'] 

In [39]: map(int, re.split(r'sur|/', grade)) 
Out[39]: [14, 20] 
+0

這個答案有點超出了提問者的水平。 – Stephan

+0

嗨。感謝你的回答;我認爲代碼可能不那麼簡短。我剛剛研究過你的;告訴我,如果我是正確的: bisect.bisect給出了斷點的等級位置,如果我們在斷點處插入等級並希望保持它的排序。 a,b = map(int,re.split(r'sur | /',grade)) 我對這個有點麻煩。我明白'sur | /'都是在年級搜索到的RE,但其他兩個字符串(由於map而變成整數)如何分配給a和b? 在'sur | /'代表什麼? – PierreOcinom

+0

關於'bisect.bisect'你完全正確。我已經添加了上面的正則表達式模式的解釋。 – unutbu

0
a,b = map(int,input ("Entrez votre note :").lower().split("sur")) 

假設輸入與您說的相符,那應該可以正常工作。

0
def printLetterGrade(rawGrade): 
    grade = rawGrade/20 
    if grade >= 0.8 : 
    print("A") 
    elif grade >= 0.6 : 
    print("B") 
    elif grade >= 0.5 : 
    print("C") 
    elif grade >= 0.4 : 
    print("D") 
    elif grade <= 0.4 : 
    print("E") 

rawGrade = int(input ("Entrez votre note entre 1 et 20:")) 
printLetterGrade(rawGrade) 

IIRC法國等級水平始終有20個爲基數,所以只告訴他們輸入他們的成績和沒有基礎,那麼你可以接受你想要的輸入。

0

這可能是有用的:

>>> s = "15 over 20" 
>>> s.split() 
['15', 'over', '20'] 

>>> [int(x) for x in s.split()] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: invalid literal for int() with base 10: 'over' 

>>> [int(x) for x in s.split() if x.isdigit()] 
[15, 20]