Python 我的基于文本的游戏中的整数检查系统不';行不通

Python 我的基于文本的游戏中的整数检查系统不';行不通,python,text,Python,Text,所以我为我的游戏制作了一个系统,这样我可以检查玩家的输入是否是整数。但出于某种原因,即使我输入一个整数,它也会返回“Please enter integer”。我相信这与此函数有关,其目的是检查玩家的输入以及它是否为整数: def pII(options): #PII stands for Player Input Int playerInput = (input("==> ")) while playerInput != range(1, options):

所以我为我的游戏制作了一个系统,这样我可以检查玩家的输入是否是整数。但出于某种原因,即使我输入一个整数,它也会返回“Please enter integer”。我相信这与此函数有关,其目的是检查玩家的输入以及它是否为整数:

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)
def pII(选项):
#PII代表玩家输入整数
playerInput=(输入(“==>”)
当玩家放范围(1,选项):
打印(“请输入一个整数”)
playerInput=(输入(“==>”)
而看跌期权:
打印(“此选项不可用”)
playerInput=int(输入(“=>”)
返回(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()
尼克·波普制作的《代码》--------------------------------------------------# 导入系统 导入操作系统 导入时间 随机输入 操作系统(“模式控制:cols=45行=80”) #功能----------------------------------------------------------# def save(): 打印(“本应保存,但还不起作用”) def clear(): 操作系统(“cls”) def pII(选项): #PII代表玩家输入整数 playerInput=(输入(“==>”) 当玩家放范围(1,选项): 打印(“请输入一个整数”) playerInput=(输入(“==>”) 而看跌期权: 打印(“此选项不可用”) playerInput=int(输入(“=>”) 返回(playerInput) #图形-----------------------------------------------------------# #所有图形函数都以“g”开头,因此我不需要任何 #我以后可能需要的名字 def gLine(): #函数画线 ps2(“********************************************************************************************************************************************************************************************************************************************************************************************************************************”) def gHeader(): gLine() ps2(“”) ___________.__ __ __ __ .__ _____ __________ .__ \__ ___/| |__ ____ / \ / \___________ _/ |_| |__ _____/ ____\ \____ /____ | | ____ ____ | | | | \_/ __ \ \ \/\/ |_ __ \__ \\ __\ | \ / _ \ __\ / /\__ \ | | / ___\ / _ \ |||Y\|uuuu/\/|uu124;\/|u124; Y\()|/124; uu/|u124;/| |____| |___| /\___ > \__/\ / |__| (____ /__| |___| / \____/|__| /_______ (____ /____|___ / \____/ \/ \/ \/ \/ \/ \/ \/ /_____/ """) gLine() def gExit(): 清除() 保存() gLine() ps2(“”) ___________ .__ __ .__ \_ _____/__ __|__|/ |_|__| ____ ____ | __)_\ \/ / \ __\ |/ \ / ___\ | \> /_______ /__/\_ \__||__| |__|___| /\___ / /\ /\ /\ /\ /\ /\ /\ \/ \/ \//_____/ \/ \/ \/ \/ \/ \/ \/""") gLine() 系统出口 def gHelp(): gLine() p2(“”) ___ ___ .__ _____ / | \ ____ | | ______ / \ ____ ____ __ __ / ~ \/ __ \| | \____ \ / \ / \_/ __ \ / \| | \ \Y | | | | | | | | | | | | | | | | | | | | | | | | |/ \___|_ / \___ >____/ __/ \____|__ /\___ >___| /____/ \/ \/ |__| \/ \/ \/ """) gLine() 打印(“欢迎使用帮助菜单!”) ps(“我稍后会写这个”) #文本功能-----------------------------------------------------# def ps(str): #这个功能允许游戏像真人一样打字 #PS是“打印速度慢”的缩写 打字速度=50 计数=0 空格=真 #此功能的这一方面与“自动图文集换行”一起使用 #确保它只在完整单词后包装,而不是 #中途 对于str中的字母: 如果字母==“”: 空格==真 其他: 空格==假 计数+=1 系统标准写入(字母) sys.stdout.flush() time.sleep(random.random()*10.0/打字速度) #此函数后面的3行作为自动图文集换行。 如果计数=100且空格=True: 打印(“\n”) 计数=0 打印(“”) def ps2(str): #此函数与PS1相同,但没有文本换行 打字速度=200 对于str中的字母: 系统标准写入(字母) sys.stdout.flush() time.sleep(random.random()*10.0/打字速度) 打印(“”) #玩家数据--------------------------------------------------------# pName=“Yorick” playerInput=0 #力学----------------------------------------------------------# #游戏循环----------------------------------------------------------# def标题屏幕(): 盖德() ps(“1.启动”) ps(“2.负载”) ps(“3.退出”) ps(“4.帮助”) 选项=4 pII(选项) 如果pII==1: start0() 如果pII==2: 负载 如果PII==3: gExit() 如果PII==4: gHelp() def start0(): 清除() ps(“你的名字,我的孩子……凡人用什么召唤你?”) pName=str(输入(“==>”) ps(pName+“?”我被低级王国的创造力逗乐了。虚空……她呼唤你,她偏爱你,就像她的神性对我一样。你……将成为我的化身。是时候了。她的预言将被填满。醒醒,我的孩子!) 标题屏幕()
问题在于使用
playerInput!=范围(1,选项)
。这总是返回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)