pycharm未导入随机Traceback(上次调用的最近一次):



python新手,尝试猜谜游戏。我在用pycharm。

这是错误后面的代码

这是我的代码:

import random
lucky number= random.randrange(1,100)
guess = int(input('guess the number in the range 1-100: '))
while guess != lucky number:
if guess < lucky number:
print ("too low, try higher")
guess = int(input("n Guess a number between 1-100")
else:
print ("too high, try lower")
guess = int(input("n Guess a number between 1-100"))
print ("you guessed the number correctly, Congratulations!")

import random
lucky number= random.randrange(1,100)
guess = int(input('guess the number in the range 1-100: '))
while guess != lucky number:
if guess < lucky number:
print ("too low, try higher")
guess = int(input("n Guess a number between 1-100")
else:
print ("too high, try lower")
guess = int(input("n Guess a number between 1-100"))
print ("you guessed the number correctly, Congratulations!")

这是错误:

Error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:Program FilesJetBrainsPyCharm Community Edition 2019.3.4pluginspython-cehelperspydev_pydev_bundlepydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
File "C:Program FilesJetBrainsPyCharm Community Edition 2019.3.4pluginspython-cehelperspydev_pydev_imps_pydev_execfile.py", line 11, in execfile
stream = tokenize.open(file)  # @UndefinedVariable
File "C:UsersShane McDonaghAppDataLocalProgramsPythonPython38-32libtokenize.py", line 392, in open
buffer = _builtin_open(filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/Shane McDonagh/.PyCharmCE2019.3/config/scratches/scratch_1.py'

FileNotFoundError:[Erno 2]没有这样的文件或目录:'C:\Users/Shane McDonagh/.PyCharmCE2019.3/config/scratch_1.py'

此错误表示您尝试运行的文件不存在。它没有说明任何关于random的内容。要修复错误,只需右键单击编辑器选项卡并选择运行。这将为该特定文件创建一个新的运行配置。如果这还不能解决问题,请删除所有运行配置,然后重试。

它表示random库不存在。这可能是由于您没有通过pip安装它,或者pycharm没有正确设置正确的配置。由于random是默认库,与大多数其他库不同,您不需要通过pip命令安装它。因此,您必须修复您的配置。O屏幕右上角的File > Settings > Project: project-name > Project Interpreter并在那里编辑您的配置,要么完全删除您的配置并创建一个新配置,要么选择一个不同的工作现有配置。

另外,不要在一个文件上多次使用同一个import语句,这可能会导致大量错误和问题,我假设您不小心粘贴了两次代码,但我想指出jsut以防发生这种情况。

我也遇到了同样的问题。在我的例子中,我在procject文件夹中创建了一个名为random.py的文件

相关内容

最新更新