【问题标题】:Why doesn't my code work on my PC, but does on others?为什么我的代码不能在我的 PC 上运行,但在其他计算机上运行?
【发布时间】:2020-01-15 15:27:37
【问题描述】:

当我运行此代码时,它会询问我输入。但是当我输入例如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 中输入输入,而不是在代码中。你是如何尝试运行你的脚本的?
  • @Chris 我只是按下在 vsc 中运行代码然后在终端中输入,但是它以某种方式依赖于输入(?)当我输入 y 时它给了我对象未找到当我输入 h 它给了我一个奇怪的列表
  • “按运行代码”是什么意思?
  • @Chris 屏幕右上角运行代码的绿色三角形
  • 该列表中没有任何奇怪,因为h 只是Get-History 的powershell 别名。见Get-Alias h

标签: python python-3.x windows


【解决方案1】:

您正在定义一个函数但没有调用它,因此当您运行代码时,它会在您输入任何内容之前完成运行。您的输入将直接发送到 PowerShell,而不是您的程序。

将此添加到脚本的底部:

average()

正如其他人指出的那样,在您的 except 分支中,您 return 您的结果而不是 printing 它。您可能想改为print(sum(nums) / len(nums))

我也强烈建议添加一个文本提示,以便更容易地区分你的程序和你的 shell,例如

float(input('Please enter a number: '))

【讨论】:

  • 是的,所以我修复了代码,它现在运行但没有按预期方式运行:def average() -> float: nums = [] while True: try: nums.append(float(input(''))) except ValueError: return sum(nums) / len(nums) average() 现在只接受输入,当我输入非浮点数时,它什么也不做没有输出
  • 函数average()返回计算值,所以调用函数时将结果保存到变量或打印返回值,否则计算无效。尝试使用print(average())
  • @Supremayro420 那是因为你没有要求它有输出...使用print
猜你喜欢
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 2016-12-13
  • 2021-03-01
相关资源
最近更新 更多