我有以下代码:
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运行它时,出现以下错误:
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
答案 0 :(得分:2)
正如@ 101所说,看来您正在Pycharm和Jupyter中运行不同版本的Python。在PyCharm中,您可以通过以下方式进行更改:
您可能会出现此错误的另一个原因是,您只是在程序期望输入数字时输入空字符串,因此请避免这样做,因为int('')
会给您您报告的错误相同:
ValueError:以10为底的int()无效文字:
答案 1 :(得分:0)
您正在输入一个空字符串,如消息所示。在转换输入之前,请先对其进行验证。像这样:
answer = input("Guess the number: ")
if not answer.isdigit(): continue
playerInput = int(answer)
这应该可以解决问题。