使用msvcrt.getch()

时间:2016-11-20 17:33:52

标签: python python-3.x getch

我正在创建一个猜测程序,允许两个玩家竞争,其中一个输入一个数字而另一个猜测答案。但是,我首先使用输入代码为用户输入一个数字,但这显示了允许第二个用户查看条目的用户输入。

Hovwever我厌倦了使用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'

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,我无法使用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()