2016-11-11 59 views
0

因此,我再一次,一如既往地一無所知。我有點新手,所以這可能比我咀嚼更多,但無論如何。 這個程序的重點是提供一個基於用戶輸入值的輸出。如果用戶沒有輸入正確的輸入,它意味着實現輸入陷阱。輸入陷阱的麻煩(其中包括)

我試圖讓一個字母或非整數值的輸入引起消息「請只輸入整數」。它適用於浮點,但不適用於字母。我應該注意到「輸入一個介於0和10之間的數字」的信息正在正常工作。 另外,當用戶輸入'done'時,循環應該關閉,但只會導致「ValueError:無法將字符串轉換爲浮點:'完成'」

我還沒有在While True格式中寫過這個,因爲它讓我更加適應這種寫while循環的方法。

setCount = 1 
allScore = 0 
done = False 
while not done: 
    strScore = float (input ("Enter Set#" + str(hwCount) + " score: ")) 
if (strScore == int (strScore) and strScore >=0 and strScore <=10): 
    totalScore = totalScore + (strScore) 
    setCount = setCount + 1 
elif (setScore == int (strScore) and(setScore < 0 or setScore > 10)): 
    print ("Please enter a number between 0 and 10.") 
elif setScore != "done": 
    print ("Please enter only whole numbers.") 
else: 
    done = True 
+0

當用戶輸入'done'時,你如何期待'float(input(..))'工作?如果您希望輸入可能不是浮點數,請不要立即轉換爲「float」!檢查它是否先完成。 –

回答

0

您立即輸入字符串轉換爲浮動在哪裏你讀同一行:

strScore = float (input ("Enter HW#" + str(hwCount) + " score: ")) 

爲了接受「完成」作爲輸入,你需要保持它作爲一個字符串,並在完成所有輸入驗證後將其轉換爲float(或int)。

drop float()和strScore將是一個字符串。然後檢查它是否等於「完成」。最後,將它轉換爲一個整數在一個try塊內

print ("Enter the homework scores one at a time. Type \"done\" when finished.") 
hwCount = 1 
totalScore = 0 
while True: 
    strScore = input ("Enter HW#" + str(hwCount) + " score: ") 
    if strScore == "done": 
     break 
    try: 
     intScore = int(strScore) 
    except ValueError: 
     print ("Please enter only whole numbers.") 
     continue 
    if (intScore >=0 and intScore <=10): 
     totalScore += intScore 
     hwCount += 1 
    else: 
     print ("Please enter a number between 0 and 10.") 
+0

Alrighty,真棒。那樣的話,我會怎麼寫呢? –

+0

@Liam我加了一個建議,根據你的例子 – maillard

0

你應該真的清理你的代碼,所有這些額外的空間傷害可讀性。我建議使用PyLint(pip install pylint,pylint file.py)。

我不會重構你的代碼太多,但你需要檢查'完成'之前轉換爲浮動。而且你會想要趕上ValueErrors,因爲有人輸入了一個無效的答案並正常處理。

print("Enter the homework scores one at a time. Type \"done\" when finished. Ctrl+c to quit at any time.") 
hwCount = 1 
totalScore = 0 
try: 
    while True: 
     strScore = input("Enter HW#" + str(hwCount) + " score: ") 
     if strScore == 'done': 
      break #done 
     else: 
      try: 
       strScore = float(strScore) 
      except ValueError: 
       print('Invalid input: must be a numerical score or "done"') 
       continue 
     if (strScore == int (strScore) and strScore >=0 and strScore <=10): 
      totalScore = totalScore + (strScore) 
      hwCount = hwCount + 1 
     elif (strScore == int (strScore) and(strScore < 0 or strScore > 10)): 
      print ("Please enter a number between 0 and 10.") 
     elif strScore != "done": 
      print ("Please enter only whole numbers.") 
except KeyboardInterrupt: 
    pass #done 

繼承人您的程序的更完整版本,僅供參考。這是我將如何重構它。

#!/usr/bin/env python3 

def findmode(lst): 
    bucket = dict.fromkeys(lst, 0) 
    for item in lst: 
     bucket[item] += 1 
    return max((k for k in bucket), key=lambda x: bucket[x]) 

print("Enter the homework scores one at a time.") 
print("Type 'done' when finished or ctrl+c to quit.") 
scores = [] 
try: 
    while True: 
     strScore = input("Enter score: ") 
     if strScore == 'done': 
      break #done 
     else: 
      try: 
       strScore = int(strScore) 
      except ValueError: 
       print('Invalid input: must be a score or "done"') 
       continue 
     if (0 <= strScore <= 10): 
      scores.append(strScore) 
     else: 
      print("Please enter a valid score between 0 and 10.") 
except KeyboardInterrupt: 
    pass # user wants to quit, ctrl+c 
finally: 
    print("Total scores graded: {}".format(len(scores))) 
    print("Highest score: {}".format(max(scores))) 
    print("Lowest score: {}".format(min(scores))) 
    print("Mean score: {}".format(sum(scores)/len(scores))) 
    print("Mode score: {}".format(findmode(scores))) 
+0

有用的信息。我將用編碼工作。非常感謝。 –

+0

@Liam我對代碼進行了一些重構,併發布瞭如何構建該程序。認爲這將是你學習的好機會。 –