2016-11-11 57 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我对代码进行了一些重构,并发布了如何构建该程序。认为这将是你学习的好机会。 –