首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python代码在Pycharm中不起作用,但在Jupiter notebook中执行良好

Python代码在Pycharm中不起作用,但在Jupiter notebook中执行良好
EN

Stack Overflow用户
提问于 2018-07-20 13:39:35
回答 2查看 728关注 0票数 0

我有以下代码:

代码语言:javascript
复制
from random import randint
pyPicInteger = randint(1,100);

print("Hello Genius, Lets introduce this game to you!\n\nThe game will get the lucky number, you just need to GUESS the luck number.\n\nThe winner will be who guess the number in the less attempts!!!")

print(pyPicInteger)

attempts=[0]

while True:
    playerInput = int(input("Guess the number: "))
    attempts.append(playerInput)
    if playerInput < 1 or playerInput > 100:
        print("Your guess is out of bound...Enter the value between 1 and 100!")
        continue
    if playerInput == pyPicInteger:
        print("YOU ARE THE WINNER NOW!!! YOU HAVE ACHEIVED IN {} ATTEMPTS".format(len(attempts)))
        break

    if attempts[-2]:
        if abs(pyPicInteger - playerInput) < abs(pyPicInteger-attempts[-2]):
            print("WARMER!")
        else:
            print("COLDER!")
    else:
        if abs(pyPicInteger-playerInput) <= 10:
            print("WARM!")
        else:
            print("COLD!")    

当我使用PyCharm运行它时,我得到以下错误:

代码语言:javascript
复制
C:\Users\*********\PycharmProjects\Operaters\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 52594 --file C:/Users/*********/PycharmProjects/Operaters/venv/GameChallenge1.py
pydev debugger: process 13700 is connecting

Connected to pydev debugger (build 181.5087.37)
Hello Genius, Lets introduce this game to you!

The game will get the lucky number, you just need to GUESS the luck number.

The winner will be who guess the number in the less attempts!!!
55
Guess the number:34
COLD!

 Guess the number: Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1664, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1658, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1068, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/*********/PycharmProjects/Operaters/venv/GameChallenge1.py", line 12, in <module>
    playerInput = int(input("Guess the number: "))
ValueError: invalid literal for int() with base 10: ''

Process finished with exit code 1
EN

回答 2

Stack Overflow用户

发布于 2018-07-20 13:49:33

正如@101所评论的,你似乎在Pycharm和Jupyter中运行不同版本的Python。

  1. 在设置/首选项对话框中,单击项目解释器。

  1. 单击齿轮图标并选择Add ...。py添加解释器添加Python解释器对话框打开。

  1. 从下拉列表中选择基本解释器,或单击browseButton并在文件系统中查找基本解释器。您应该给出使用jupyter的解释器的路径。

出现此错误的另一个原因是,当程序需要一个数字时,您只是输入了一个空字符串,因此请避免这样做,因为int('')会给出与您报告的错误相同的错误:

ValueError:基数为10的int()的文本无效:''

票数 2
EN

Stack Overflow用户

发布于 2018-07-20 19:53:45

正如消息所说,您正在输入一个空字符串。在转换输入之前,请先验证输入。类似于:

代码语言:javascript
复制
answer = input("Guess the number: ")
if not answer.isdigit(): continue
playerInput = int(answer)

这应该能起到作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51435732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档