为什么我的代码在我的PC上不起作用,而在其他PC上却不起作用?

时间:2020-01-15 15:27:37

标签: python python-3.x windows

运行此代码时,它会要求我输入。但是当我输入实例y时,它给了我一个objectnotfound错误,当我输入h时,给了我一些列表,该列表比整个代码的长度还长。

我尝试将这段代码发送给Python不和谐的人,看来对他们有用。

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上使用Visual Studio Code的最新32位版本。我也在Python IDLE中尝试了该代码,但这似乎效果不佳。

1 个答案:

答案 0 :(得分:0)

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

将此添加到脚本的底部:

average()

正如其他人指出的那样,在您的except分支中,您return而不是print对其结果进行编码。您可能想改用print(sum(nums) / len(nums))

我还强烈建议添加文本提示,例如,以便更轻松地区分程序和Shell。

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