我似乎有一个问题,使用sublimeREPL运行python代码与用户输入sublime文本2。我有一段代码可以在命令提示符中使用,它不会在REPL中执行。错误似乎是REPL无法处理输入的格式,并假定它是一个字符串。我的python相当有限,所以有没有办法让REPL更好地处理我的代码,或者我需要指定输入?你知道吗
注意:每次将tempInput转换为int都可以,但是很繁琐。你知道吗
代码:
# Matthew P
# A program to calculate average grades
def avg(total, elements):
return total / elements
tempInput = 0
runningTot = 0
numGrades = 0
print("\nEnter Grades (Negative Value to Escape): ")
while tempInput > -1:
tempInput = input("-->")
if tempInput > -1:
runningTot = runningTot + tempInput
numGrades = numGrades + 1
print("\nQuit Command Givem")
print("Average Grade: " + str(avg(runningTot,numGrades)))
print("Grade Sum: " + str(runningTot))
print("Number of Grades" + str(numGrades))
命令提示符的输出:
~\Documents\Python Scripts>userinput.py
Enter Grades (Negative Value to Escape):
-->99
-->98
-->97
-->96
-->95
-->-1
Quit Command Givem
Average Grade: 97
Grade Sum: 485
Number of Grades 5
以及在sublimeREPL中运行时的错误(我运行时使用的是ctrl+,,f命令)
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Enter Grades (Negative Value to Escape):
-->100
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 17, in <module>
TypeError: unorderable types: str() > int()
>>>
input()
返回一个字符串,但您正在将其与整数进行比较:使用
int()
进行比较:您使用python2在命令行上运行代码,其中
input()
将输入的字符串作为Python表达式进行计算。python2也不介意比较数字和字符串;数字总是在数字之前排序。你知道吗然而,在Sublime中,您在python3下运行了代码,其中
input()
仅接受字符串输入。你知道吗相关问题 更多 >
编程相关推荐