Python 使用msvcrt.getch()
我正在创建一个猜测程序,允许两个玩家竞争,其中一个输入数字,另一个猜测答案。然而,一开始我使用用户输入代码来输入一个数字,但这显示了允许第二个用户查看输入的用户输入 尽管我厌倦了使用numberGuess=msvcrt.getch(),但结果如下所示。我应该怎么做才能在不出错的情况下对numberGuess执行相同的检查?以及用户条目被替换为“*” 我的代码:Python 使用msvcrt.getch(),python,python-3.x,getch,Python,Python 3.x,Getch,我正在创建一个猜测程序,允许两个玩家竞争,其中一个输入数字,另一个猜测答案。然而,一开始我使用用户输入代码来输入一个数字,但这显示了允许第二个用户查看输入的用户输入 尽管我厌倦了使用numberGuess=msvcrt.getch(),但结果如下所示。我应该怎么做才能在不出错的情况下对numberGuess执行相同的检查?以及用户条目被替换为“*” 我的代码: import msvcrt from random import randint import struct def HumanAgai
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()
导入msvcrt
从随机导入randint
导入结构
def HumanAgainstHuman():
changemax=input(“是否要更改最大值?”)
如果changemax.lower()=“是”:
maxNumber=int(输入(“输入新的最大值:”)
其他:
maxNumber=9
numberGuess=msvcrt.getch()
numbergures=int(numberGuess)
当numberGuess<1或numberGuess>maxNumber时:
numberGuess=input(“不是有效的选择,请输入另一个数字:\n”)。替换
猜测=0
numberguesss=0
猜猜看!=numberGuess和numberGuess<3:
guess=int(输入(“玩家二猜:\n”))
numberguesss=numberguesss+1
如果guess==numberGuess:
打印(“玩家两胜”)
其他:
打印(“玩家一赢”)
再次播放()
def choosingGame():
Choice=int(输入(“选择…\n 1表示人对人\n 2表示人对人工智能\n 3表示人工智能对人工智能”))
当选项<1或选项>3时:
Choice=int(输入(“重试…选择…\n 1表示人对人\n 2表示人对AI \n 3表示AI对AI”))
如果选项==1:
人类对抗人类
elif选项==2:
HagainstAI()
elif选项==3:
艾甘斯坦()
def playreach():
回答=int(输入(“按1再次播放或按任何其他数字结束”))
如果答案=1:
选择游戏()
其他:
打印(“再见!”)
尝试:
输入(“按回车键终止程序”)
除SyntaxError外:
通过
选择游戏()
运行程序时的结果
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'
选择。。。
1人对人
2人Vs人工智能
人工智能与人工智能之比较
1.
您想更改最大值吗?不
回溯(最近一次呼叫最后一次):
文件“C:/Users/Sarah/Documents/testing.py”,第55行,在
选择游戏()
choosingGame中第38行的文件“C:/Users/Sarah/Documents/testing.py”
人类对抗人类
文件“C:/Users/Sarah/Documents/testing.py”,第14行,HumanAgainstHuman
ValueError:以10为基数的int()的文本无效:b'\xff'
正如我在评论中所说,我无法重现您在getch()
中遇到的问题。也就是说,下面是HumanAgainstHuman()
函数的改进版(但仍不完善),该函数演示了一种使用getch()
的方法,可以防止遇到的问题类型
该函数还有一个问题,即它试图在分配变量guess
之前引用变量的值-但是,由于我无法准确理解您试图执行的操作,该问题仍然存在于待解决的代码中
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()
try:
numberGuess = int(numberGuess)
except ValueError:
numberGuess = 0 # assign it some invalid number
while numberGuess < 1 or numberGuess > maxNumber:
numberGuess = input("Not a valid choice, please enter another number:\n")
guess = 0
numberGuesses = 0
while guess != numberGuess and numberGuesses < 3: ## different problem here!
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 HumanAgainstHuman():
changemax=input(“是否要更改最大值?”)
如果changemax.lower()=“是”:
maxNumber=int(输入(“输入新的最大值:”)
其他:
maxNumber=9
numberGuess=msvcrt.getch()
尝试:
numberGuess=int(numberGuess)
除值错误外:
numberGuess=0#给它分配一些无效的数字
当numberGuess<1或numberGuess>maxNumber时:
numberGuess=input(“不是有效的选择,请输入另一个数字:\n”)
猜测=0
numberguesss=0
猜猜看!=numberGuess和numberGuess<3:##这里有不同的问题!
guess=int(输入(“玩家二猜:\n”))
numberguesss=numberguesss+1
如果guess==numberGuess:
打印(“玩家两胜”)
其他:
打印(“玩家一赢”)
再次播放()
正如我在评论中所说,我无法重现您在getch()
中遇到的问题。也就是说,下面是HumanAgainstHuman()
函数的改进版(但仍不完善),该函数演示了一种使用getch()
的方法,可以防止遇到的问题类型
该函数还有一个问题,即它试图在分配变量guess
之前引用变量的值-但是,由于我无法准确理解您试图执行的操作,该问题仍然存在于待解决的代码中
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()
try:
numberGuess = int(numberGuess)
except ValueError:
numberGuess = 0 # assign it some invalid number
while numberGuess < 1 or numberGuess > maxNumber:
numberGuess = input("Not a valid choice, please enter another number:\n")
guess = 0
numberGuesses = 0
while guess != numberGuess and numberGuesses < 3: ## different problem here!
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 HumanAgainstHuman():
changemax=input(“是否要更改最大值?”)
如果changemax.lower()=“是”:
maxNumber=int(输入(“输入新的最大值:”)
其他:
maxNumber=9
numberGuess=msvcrt.getch()
尝试:
numberGuess=int(numberGuess)
除值错误外:
numberGuess=0#给它分配一些无效的数字
当numberGuess<1或numberGuess>maxNumber时:
numberGuess=input(“不是有效的选择,请输入另一个数字:\n”)
猜测=0
numberguesss=0
猜猜看!=numberGuess和numberGuess<3:##这里有不同的问题!
guess=int(输入(“玩家二猜:\n”))
numberguesss=numberguesss+1
如果guess==numberGuess:
打印(“玩家两胜”)
其他:
打印(“玩家一赢”)
再次播放()
我不认为您打算在其中包含numbergures
…您使用的是Python 2还是Python 3?您按了什么键来获取ValueError
?@martineau python 3。5@PM2Ring你的意思是我应该键入msvcrt.getch()而不是numberGuess=msvcrt.getch(),只有这样做了,我才能对照其他播放器检查用户输入。PM2Ring表示numbergures=int(numberGuess)
。我不知道