简体   繁体   English

为什么我的代码在我的电脑上不起作用,但在其他人身上?

[英]Why doesn't my code dont work on my pc, but does on others?

When I run this code it asks me for inputs.当我运行此代码时,它会要求我输入。 But when I input for instance y , it gives me an objectnotfound error, and when I input h , it gives me some list which is longer than the entirety of code.但是当我输入例如y ,它给了我一个 objectnotfound 错误,当我输入h ,它给了我一些比整个代码更长的列表。

I tried sending this code to someone else in the Python discord and it seemed like it worked for them.我尝试将此代码发送给 Python discord 中的其他人,似乎对他们有用。

def average() -> float:
    nums = []
    while True:
        try:
            nums.append(float(input('')))
        except ValueError:
            return sum(nums) / len(nums)

When I input y :当我输入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

When I input h :当我输入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


I'm using the latest 32 bit release of Visual Studio Code on a 32 bit Windows 10 Pro.我在 32 位 Windows 10 Pro 上使用最新的 32 位版本的 Visual Studio Code。 I also tried that code in Python IDLE, this didn't seem to work as well.我也在 Python IDLE 中尝试了该代码,这似乎也不起作用。

You're defining a function but not calling it, so when you run your code it finishes running before you type anything.您正在定义一个函数而不是调用它,因此当您运行代码时,它会在您输入任何内容之前完成运行。 Your input it being sent directly to PowerShell, not to your program.您的输入将直接发送到 PowerShell,而不是您的程序。

Add this to the bottom of your script:将此添加到脚本的底部:

average()

As others have pointed out, in your except branch you return your result instead of print ing it.正如其他人指出的那样,在您的except分支中,您return结果而不是print它。 You probably want to print(sum(nums) / len(nums)) instead.你可能想print(sum(nums) / len(nums))代替。

I also strongly suggest adding a text prompt to more easily distinguish between your program and your shell, eg我还强烈建议添加一个文本提示来更容易地区分你的程序和你的 shell,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM