我有以下代码:
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发布于 2018-07-20 13:49:33
正如@101所评论的,你似乎在Pycharm和Jupyter中运行不同版本的Python。


出现此错误的另一个原因是,当程序需要一个数字时,您只是输入了一个空字符串,因此请避免这样做,因为int('')会给出与您报告的错误相同的错误:
ValueError:基数为10的int()的文本无效:''
发布于 2018-07-20 19:53:45
正如消息所说,您正在输入一个空字符串。在转换输入之前,请先验证输入。类似于:
answer = input("Guess the number: ")
if not answer.isdigit(): continue
playerInput = int(answer)这应该能起到作用。
https://stackoverflow.com/questions/51435732
复制相似问题