![](/img/trans.png)
[英]when i run pip from cmd on python 3.9 i just installed on windows 10, it gives a wierd error and stops
[英]Python gives error if I run this code from cmd, however runs fine if I run it from Jupyter
基本上,我正在阅读列表中的输入。 如果它不是整数,它应该给出一个错误,并在我写“完成”时跳过该输入并停止。 然后我创建一个计数、总和和平均值,并打印出来。
total = 0
count = 0
list = []
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
fnum = float(num)
list.append(fnum)
except:
print("Invalid input")
print(fnum, type(fnum))
continue
print(list)
for i in list:
count += 1
total += i
print(total, count, "Average: ", total/count)
就像我说的,它在 Jupyter 或 Colab 上运行良好,但我从 cmd 收到以下错误消息:
Traceback (most recent call last):
File "C:location\file.py", line 6, in <module>
num = input("Enter a number: ")
File "<string>", line 1, in <module>
NameError: name 'asd' is not defined
Traceback (most recent call last):
File "C:location\file.py", line 6, in <module>
num = input("Enter a number: ")
File "<string>", line 1, in <module>
NameError: name 'done' is not defined
可能您正在使用 Python 2 运行它。 Jupyter 使用 Python 3,所以没问题。 但是在 python2 中, input()
函数接受一个输入并将其作为代码执行。 您输入了asd
- 并且 python 抱怨没有asd
变量(与done
相同)。 使用 python 3 运行它,或使用raw_input()
函数,它与 python 3 中的input()
具有相同的效果 - 但在 python 2 中(即没有运行代码)。
假设您使用python filename.py
来运行您的代码 - 尝试python -V
。 我给你python版本。 如果我正确的话,大多数情况下您可以使用python3
而不是python
访问 python3 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.