我的基于文本的游戏中的整数检查系统不起作用

问题描述 投票:0回答:1

所以我为我的游戏制作了一个系统,以便我可以检查玩家的输入是否为整数。但由于某种原因,即使我输入一个整数,它也会返回“请输入整数”。我相信它在这里与这个功能有关,其目的是检查玩家输入以及它是否是整数:

def pII(options):
    #PII stands for Player Input Int 
    playerInput = (input("==> "))

    while playerInput != range(1, options):
        print("Please enter an integer")
        playerInput = (input("==>"))  

    while playerInput <= 0 or playerInput > options:
        print("This is not an available option")
        playerInput = int(input("==>"))    

    return(playerInput)

逻辑对我有意义,所以我不明白为什么它不起作用。我没有得到任何错误,所以至少就是这样。无论如何,如果有帮助,这里是我游戏的完整代码。我采取的代码段落来自第15-27行

#Made by Nick Pope--------------------------------------------------#
import sys
import os
import time
import random

os.system("mode con: cols=45 lines=80")
#Functions----------------------------------------------------------#
def save():
    print("                   This is supposed to save, but it doesn't work yet")

def clear():
    os.system('cls')

def pII(options):
    #PII stands for Player Input Int 
    playerInput = (input("==> "))

    while playerInput != range(1, options):
        print("Please enter an integer")
        playerInput = (input("==>"))  

    while playerInput <= 0 or playerInput > options:
        print("This is not an available option")
        playerInput = int(input("==>"))    

    return(playerInput)
#Graphics-----------------------------------------------------------#
  #All Graphic functions start with "g" so that i don't take any 
  #names I might need later
def gLine():
    #Function Draws lines 
    ps2("******************************************************************************************************************")

def gHeader():
    gLine()
    ps2(""" 
___________.__              __      __               __  .__             _____  __________      .__                 
\__    ___/|  |__   ____   /  \    /  \___________ _/  |_|  |__    _____/ ____\ \____    /____  |  |   ____   ____  
  |    |   |  |  \_/ __ \  \   \/\/   |_  __ \__  \\   __\  |  \   /  _ \   __\    /     /\__  \ |  |  / ___\ /  _ \ 
  |    |   |   Y  \  ___/   \        / |  | \// __ \|  | |   Y  \ (  <_> )  |     /     /_ / __ \|  |_/ /_/  >  <_> )
  |____|   |___|  /\___  >   \__/\  /  |__|  (____  /__| |___|  /  \____/|__|    /_______ (____  /____|___  / \____/ 
                \/     \/         \/              \/          \/                         \/    \/    /_____/         """)
    gLine() 

def gExit():
    clear()
    save()
    gLine()
    ps2("""
___________      .__  __  .__                                     
\_   _____/__  __|__|/  |_|__| ____    ____                       
 |    __)_\  \/  /  \   __\  |/    \  / ___\                      
 |        \>    <|  ||  | |  |   |  \/ /_/  >                     
/_______  /__/\_ \__||__| |__|___|  /\___  / /\ /\ /\ /\ /\ /\ /\ 
        \/      \/                \//_____/  \/ \/ \/ \/ \/ \/ \/""")
    gLine()
    sys.exit

def gHelp():
    gLine()
    p2("""
  ___ ___        .__              _____                       
 /   |   \  ____ |  | ______     /     \   ____   ____  __ __ 
/    ~    \/ __ \|  | \____ \   /  \ /  \_/ __ \ /    \|  |  \
\    Y    |  ___/|  |_|  |_> > /    Y    \  ___/|   |  \  |  /
 \___|_  / \___  >____/   __/  \____|__  /\___  >___|  /____/ 
       \/      \/     |__|             \/     \/     \/    """)
    gLine()
    print("Welcome to the help menu!")
    ps("I will write this later")
#Text Functions-----------------------------------------------------#

def ps(str):
    #This function allows the game to type like a real person 
    #PS is short for "Print Slow"
    typing_speed = 50
    count = 0
    space = True
    #This aspect of the function works with the autotext wrap
    #To make sure that it only wraps after full words, not 
    #midway through 
    for letter in str:
        if letter == " ":
            space == True
        else:
            space == False

        count += 1
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(random.random()*10.0/typing_speed)
        #The 3 lines after this function as an autotext wrap.
        if count == 100 and space == True:
            print('\n')
            count = 0
    print('')

def ps2(str):
    #This function is the same as PS1 but without the Text Wrapping 
    typing_speed = 200
    for letter in str:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(random.random()*10.0/typing_speed)
    print('')
#Player Data--------------------------------------------------------#
pName = "Yorick"
playerInput = 0
#Mechanics----------------------------------------------------------#

#Game Loop----------------------------------------------------------#

def titleScreen():
    gHeader()
    ps("1. Start")
    ps("2. Load")
    ps("3. Exit")
    ps("4. Help")
    options = 4

    pII(options)

    if pII == 1:
        start0()
    if pII == 2:
        load
    if PII == 3:
        gExit()
    if PII == 4:
        gHelp()

def start0():
    clear()
    ps("Your name my child..what do the mortals use to summon you?")
    pName = str(input("==>"))
    ps(pName +"?" " I amused by the creativity of the lower realms. The void..she calls to you, she favors you, just as her divinity does to me. You..shall be my avatar. It is time. Her prophecy shall be fullfilled. Awaken, my child!")






titleScreen()
python text
1个回答
1
投票

问题来自测试playerInput != range(1, options)选项是否正确。这总是返回true,因为playerInput永远不是一个范围。比较应该是:

playerInput not in range(1, options)

将playerInput与范围中的每个值进行比较

请记住,我们还希望将输入作为整数,因此该类变为:

def pII(options):
    #PII stands for Player Input Int 
    playerInput = (input("==> "))

    while playerInput not in  range(0,options):
        print("Please enter an integer")
        playerInput = int(input("==>"))  

    while playerInput <= 0 or playerInput > options:
        print("This is not an available option")
        playerInput = int(input("==>"))    

    return(playerInput)
© www.soinside.com 2019 - 2024. All rights reserved.