检查输入在python中是一个数字

检查输入在python中是一个数字,python,Python,我需要帮助,我的程序正在模拟骰子的动作。我想做一个错误检查,检查输入字符串是否是数字,如果不是,我想再次问这个问题,直到他输入一个整数 # This progam will simulate a dice with 4, 6 or 12 sides. import random def RollTheDice(): print("Roll The Dice") print() NumberOfSides = int(input("Please select a

我需要帮助,我的程序正在模拟骰子的动作。我想做一个错误检查,检查输入字符串是否是数字,如果不是,我想再次问这个问题,直到他输入一个整数

# This progam will simulate a dice with 4, 6 or 12 sides.

import random 

def RollTheDice():

    print("Roll The Dice")
    print()


    NumberOfSides = int(input("Please select a dice with 4, 6 or 12 sides: "))

    Repeat = True 

    while Repeat == True:


        if not NumberOfSides.isdigit() or NumberOfSides not in ValidNumbers:
            print("You have entered an incorrect value")
            NumberOfSides = int(input("Please select a dice with 4, 6 or 12 sides")

        print()
        UserScore = random.randint(1,NumberOfSides)
        print("{0} sided dice thrown, score {1}".format (NumberOfSides,UserScore))

        RollAgain = input("Do you want to roll the dice again? ")


        if RollAgain == "No" or RollAgain == "no":
            print("Have a nice day")
            Repeat = False

        else:
            NumberOfSides = int(input("Please select a dice with 4, 6 or 12 sides: "))

一位评论者不喜欢我的第一个回答,即“尝试:
”,除了ValueError
,OP询问如何使用
isdigit
,这就是你可以做到的:

valid_numbers = [4, 6, 12]
while repeat:
    number_of_sides = 0      
    while number_of_sides not in valid_numbers:
          number_of_sides_string = input("Please select a dice with 4, 6 or 12 sides: ")
          if (not number_of_sides_string.strip().isdigit() 
              or int(number_of_sides_string) not in valid_numbers):
              print ("please enter one of", valid_numbers)
          else:
              number_of_sides = int(number_of_sides_string)
    # do things with number_of_sides
有趣的一行是
而不是边数\u string.strip().isdigit()
。为方便起见,
条带
删除输入字符串两端的空白。然后,
isdigit()
检查整个字符串是否由数字组成

在您的情况下,您只需检查

 if not number_of_sides_string not in ['4', '6', '12']:
     print('wrong')
但如果你想接受任何数字,另一种解决方案更为普遍


另一方面,建议使用小写下划线分隔变量名。

作为评论员,我不喜欢我的第一个回答
尝试:
,除了ValueError
,OP询问如何使用
isdigit
,这就是你可以做到的:

valid_numbers = [4, 6, 12]
while repeat:
    number_of_sides = 0      
    while number_of_sides not in valid_numbers:
          number_of_sides_string = input("Please select a dice with 4, 6 or 12 sides: ")
          if (not number_of_sides_string.strip().isdigit() 
              or int(number_of_sides_string) not in valid_numbers):
              print ("please enter one of", valid_numbers)
          else:
              number_of_sides = int(number_of_sides_string)
    # do things with number_of_sides
有趣的一行是
而不是边数\u string.strip().isdigit()
。为方便起见,
条带
删除输入字符串两端的空白。然后,
isdigit()
检查整个字符串是否由数字组成

在您的情况下,您只需检查

 if not number_of_sides_string not in ['4', '6', '12']:
     print('wrong')
但如果你想接受任何数字,另一种解决方案更为普遍


另外,建议使用小写下划线分隔变量名。

捕获变量中的字符串,例如
text
。然后执行
if text.isdigit()
捕获变量中的字符串,比如
text
。然后执行
如果text.isdigit()

使用以下内容创建函数:

while NumberOfSides != 4 and NumberOfSides != 6 and NumberOfSides != 12:
    print("You have selected the wrong sided dice")
    NumberOfSides = int(input("Please select a dice with 4, 6 or 12 sides: "))

并在需要输入时调用它。您还应提供退出选项,例如按0。您还应该尝试捕捉无效的数字。有一个确切的答案。请注意,输入总是尝试将其解析为一个数字,并且会引发自身的异常。

使用以下内容创建函数:

while NumberOfSides != 4 and NumberOfSides != 6 and NumberOfSides != 12:
    print("You have selected the wrong sided dice")
    NumberOfSides = int(input("Please select a dice with 4, 6 or 12 sides: "))
并在需要输入时调用它。您还应提供退出选项,例如按0。您还应该尝试捕捉无效的数字。有一个确切的答案。请注意,输入总是试图将其解析为一个数字,并且会引发自身的异常。

您可以使用

或更多的“pythonic”:

你可以用

或更多的“pythonic”:


你是在使用python2还是python3?我知道这是你的第一个python程序之一,但请不要像用C一样用python编写…你是在使用python2还是python3?我知道这是你的第一个python程序之一,但请不要像用C一样用python编写…我不喜欢在这里使用流控制的异常处理。更确切地说,它是对
text.isdigit()
之类的东西的调用,它处理带有前导或尾随空格的输入。“12.isdigit()是假的我知道,我只是想知道
text.strip().isdigit()
是否真的比try-except更可读。好吧,如果我使用'isdigit()'函数,我该如何将其放入我的代码中我尝试了你的代码,但它不起作用。你能告诉我整个代码吗?我不喜欢在这里使用异常处理来进行流控制。更确切地说,它是对
text.isdigit()
之类的东西的调用,它处理带有前导或尾随空格的输入。“12.isdigit()是错误的我知道,我只是想知道
text.strip().isdigit()
是否真的比try-except更可读。好的,如果我使用'isdigit()'函数,我该如何将其放入我的代码中我尝试了你的代码,但它不起作用。你能给我整个代码吗如果你想更宽容用户的输入,你可以
strip()
先输入。如果您想更宽容用户输入,可以
strip()
先输入。