【问题标题】:Using msvcrt.getch()使用 msvcrt.getch()
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-04-11
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多