python - 为什么我的代码不能在我的 PC 上运行,但在其他计算机上运行?
问题描述
当我运行此代码时,它要求我输入。但是,例如y
,当我输入时,它给了我一个“objectnotfound 错误”,当我输入时h
,它给了我一些比整个代码更长的列表。
我尝试将此代码发送给 Python Discord频道中的其他人,并且似乎对他们有用。
def average() -> float:
nums = []
while True:
try:
nums.append(float(input('')))
except ValueError:
return sum(nums) / len(nums)
当我输入y
:
PS C:\Users\NPC> 7
7
PS C:\Users\NPC> 5
5
PS C:\Users\NPC> 1
1
PS C:\Users\NPC> 51.56
51,56
PS C:\Users\NPC> y
y : The term 'y' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ y
+ ~
+ CategoryInfo : ObjectNotFound: (y:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
当我输入h
:
PS C:\Users\NPC> 6
6
PS C:\Users\NPC> 62.32
62,32
PS C:\Users\NPC> 5123.412
5123,412
PS C:\Users\NPC> h
Id CommandLine
-- -----------
1 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
2 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
3 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
4 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
5 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
6 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
7 4
8 5
9 6
10 y
11 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
12 6
13 5
14 34
15 h
16 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
17 y
18 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
19 5
20 2
21 5
22 d
23 5
24 2
25 5
26 print(nums)
27 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
28 4
29 5
30 6
31 y
32 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
33 7
34 5
35 1
36 51.56
37 y
38 & C:/Users/NPC/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/NPC/Desktop/python projects/arithm...
39 6
40 62.32
41 5123.412
我在 32 位 Windows 10 Pro 系统上使用最新的 32 位版本的 Visual Studio Code。我还在 Python IDLE中尝试了该代码,但这似乎也不起作用。
解决方案
您正在定义一个函数但没有调用它,因此当您运行代码时,它会在您输入任何内容之前完成运行。您的输入将直接发送到 PowerShell,而不是您的程序。
将此添加到脚本的底部:
average()
正如其他人指出的那样,在您的except
分支中,您return
的结果而不是结果print
。你可能想print(sum(nums) / len(nums))
改为。
我还强烈建议添加一个文本提示,以便更轻松地区分您的程序和您的 shell,例如
float(input('Please enter a number: '))
推荐阅读
- java - 将放心的@Steps注入spring-boot测试时出现空指针异常
- reactjs - 使用钩子反应兄弟组件之间的传递道具
- javascript - 从一个 Javascript 函数重定向到另一个脚本文件中的不同 Javascript 函数
- flutter - 如何从 RaisedButton 打开 dropDownButton
- c++ - Windows 系统中的最大可用线程数?C++
- python - ValueError:未知标签类型:同时使用聚类+分类模型时为“连续”
- java - 如何更改链表中的元素?
- go - 添加列时去矩阵变异
- c# - WPF 嵌套用户控件数据上下文
- python - python中有没有办法获取列表并将其转换为字典?