如何只接受3-8之间的整数而不接受字符串?(Python 2.7.5)

如何只接受3-8之间的整数而不接受字符串?(Python 2.7.5),python,python-2.7,while-loop,conditional-statements,Python,Python 2.7,While Loop,Conditional Statements,我正在尝试创建一个函数,该函数要求3-8之间的整数,并将继续询问,直到用户输入3-8之间的整数。因此,它会再次询问您是否输入0、-1、9或“兔子” 到目前为止,我有: def GetNumberOfColours(): NumberOfColours = None while type(NumberOfColours) != int or int(NumberOfColours) < 3 or int(NumberOfColours) > 8: pri

我正在尝试创建一个函数,该函数要求3-8之间的整数,并将继续询问,直到用户输入3-8之间的整数。因此,它会再次询问您是否输入0、-1、9或“兔子”

到目前为止,我有:

def GetNumberOfColours():
    NumberOfColours = None
    while type(NumberOfColours) != int or int(NumberOfColours) < 3 or int(NumberOfColours) > 8:
        print "Please enter the amount of colours you would like to play with (min 3, max 8)."
        NumberOfColours = raw_input()
    NumberOfColours = int(NumberOfColours)
def getnumberofcolors():
numberofcolors=无
while type(numberofcolors)!=int或int(numberofcolors)<3或int(numberofcolors)>8:
打印“请输入您想要使用的颜色数量(最少3种,最多8种)。”
NumberOfColor=原始输入()
numberofcolors=int(numberofcolors)
但这段代码目前无法工作,因为它接受原始输入,如果是整数,则不会将其视为整数。但是如果我使用input(),它将不会接受可以输入的字符串并停止代码。 我如何才能做到这一点?

type(numberofcolors)
在第一次运行时总是
str
(或
NoneType
),因为
raw\u input()
返回字符串

你应该这样做:

def get_number_of_colours():
    while True:
        print "Please enter the amount of colours you would like to play with (min 3, max 8):",
        try:
            num_colours = int(raw_input())
        except ValueError:  # gets thrown on any input except an integer value
            continue
        if 3 <= num_colours <= 8:
            return num_colours
def获取颜色的数量()
尽管如此:
打印“请输入您想要使用的颜色数量(最少3种,最多8种):”,
尝试:
num\u colors=int(原始输入()
Exception ValueError:#在除整数值之外的任何输入上抛出
持续
如果3
type(numberofcolors)
在第一次运行时总是
str
(或
NoneType
),因为
raw\u input()
返回字符串

你应该这样做:

def get_number_of_colours():
    while True:
        print "Please enter the amount of colours you would like to play with (min 3, max 8):",
        try:
            num_colours = int(raw_input())
        except ValueError:  # gets thrown on any input except an integer value
            continue
        if 3 <= num_colours <= 8:
            return num_colours
def获取颜色的数量()
尽管如此:
打印“请输入您想要使用的颜色数量(最少3种,最多8种):”,
尝试:
num\u colors=int(原始输入()
Exception ValueError:#在除整数值之外的任何输入上抛出
持续

如果3您需要缩进最后一行,让脚本反复将输入转换为整数

然后,您会发现输入“兔子”将产生一个
ValueError
,因为
int()
无法将其转换为数字。这可以通过以下方法处理:

def getnumberofcolors():
numberofcolors=无
while type(numberofcolors)!=int或int(numberofcolors)<3或int(numberofcolors)>8:
打印“请输入您想要使用的颜色数量(最少3种,最多8种)。”
NumberOfColor=原始输入()
尝试:
numberofcolors=int(numberofcolors)
除值错误外:
numberofcolors=无

您需要缩进最后一行,让脚本反复将输入转换为整数

然后,您会发现输入“兔子”将产生一个
ValueError
,因为
int()
无法将其转换为数字。这可以通过以下方法处理:

def getnumberofcolors():
numberofcolors=无
while type(numberofcolors)!=int或int(numberofcolors)<3或int(numberofcolors)>8:
打印“请输入您想要使用的颜色数量(最少3种,最多8种)。”
NumberOfColor=原始输入()
尝试:
numberofcolors=int(numberofcolors)
除值错误外:
numberofcolors=无

将此用于您的while行:

while !NumberOfColors.isDigit() or int(NumberOfColours) < 3 or int(NumberOfColours) > 8:
while!NumberOfColors.isDigit()或int(NumberOfColors)<3或int(NumberOfColors)>8:

将此用于您的while行:

while !NumberOfColors.isDigit() or int(NumberOfColours) < 3 or int(NumberOfColours) > 8:
while!NumberOfColors.isDigit()或int(NumberOfColors)<3或int(NumberOfColors)>8: