【发布时间】:2016-11-20 17:33:51
【问题描述】:
我正在创建一个猜谜程序,允许两个玩家竞争,其中一个输入一个数字,另一个猜出答案。然而,起初我使用输入代码让用户输入一个数字,但这显示了允许第二个用户查看条目的用户输入。
但是我厌倦了使用 numberGuess = msvcrt.getch() 结果我得到了如下所示的结果。我应该怎么做才能对 numberGuess 执行相同的检查而不会出错?以及用户条目被替换为“*”
我的代码:
import msvcrt
from random import randint
import struct
def HumanAgainstHuman ():
changemax = input("Would you like to change the maximum?")
if changemax.lower() == "yes":
maxNumber = int(input("Enter the new max:"))
else:
maxNumber = 9
numberGuess = msvcrt.getch()
nuberGuress= int(numberGuess)
while numberGuess < 1 or numberGuess > maxNumber:
numberGuess = input("Not a valid choice, please enter another number: \n").replace
guess = 0
numberGuesses = 0
while guess != numberGuess and numberGuesses < 3:
guess = int(input("Player Two have a guess: \n"))
numberGuesses = numberGuesses + 1
if guess == numberGuess:
print("Player Two wins")
else:
print("Player One wins")
PlayAgain()
def choosingGame():
Choice = int(input("Choose...\n 1 for Human Vs Human \n 2 for Human Vs AI \n 3 for AI Vs AI \n"))
while Choice < 1 or Choice > 3:
Choice = int(input("Try again...Choose...\n 1 for Human Vs Human \n 2 for Human Vs AI \n 3 for AI Vs AI \n"))
if Choice == 1:
HumanAgainstHuman()
elif Choice == 2:
HagainstAI()
elif Choice == 3:
AIagainstAI()
def PlayAgain():
answer = int(input("Press 1 to play again or Press any other number to end"))
if answer == 1:
choosingGame()
else:
print("Goodbye!")
try:
input("Press enter to kill program")
except SyntaxError:
pass
choosingGame()
运行程序的结果
Choose...
1 for Human Vs Human
2 for Human Vs AI
3 for AI Vs AI
1
Would you like to change the maximum?no
Traceback (most recent call last):
File "C:/Users/Sarah/Documents/testing.py", line 55, in <module>
choosingGame()
File "C:/Users/Sarah/Documents/testing.py", line 38, in choosingGame
HumanAgainstHuman()
File "C:/Users/Sarah/Documents/testing.py", line 14, in HumanAgainstHuman
ValueError: invalid literal for int() with base 10: b'\xff'
【问题讨论】:
-
我不认为你打算在里面有
nuberGuress... -
您使用的是 Python 2 还是 Python 3?你按什么键得到
ValueError? -
@martineau python 3.5
-
@PM2Ring 你的意思是不是 numberGuess = msvcrt.getch() 我应该输入 msvcrt.getch() 只有当我这样做时我才能检查用户输入与其他玩家。
-
PM2Ring 表示
nuberGuress= int(numberGuess)。
标签: python python-3.x getch