簡體   English   中英

為什么我的代碼在我的電腦上不起作用,但在其他人身上?

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

當我運行此代碼時,它會要求我輸入。 但是當我輸入例如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: '))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM