简体   繁体   English

要求输入10位数字

[英]Asking for 10-digit numbers

I am testing that the user input is of length 10 and only contains numbers. 我正在测试用户输入的长度为10,并且仅包含数字。 At the moment, my code is: 目前,我的代码是:

while True:
    number = input("Enter number: ")

    try:
        if len(number) != 10:
        print ("Enter 10 digits\n")
        continue

    except ValueError:
        print ("Enter only numbers\n")
        continue

    else: 
        break

The program will ask for user input, then test that it is of 10 length and only contains integers. 该程序将要求用户输入,然后测试其长度为10,并且仅包含整数。 Currently, user input is read as a string so that if it began with a '0', then this would be included in len(), if you know what I mean? 当前,用户输入被读取为字符串,因此,如果它以“ 0”开头,那么如果您知道我的意思,它将被包含在len()中。 For example, if I inputted '0123456789', this would be seen to have a length of 10 and not 9 because it begins with '0'. 例如,如果我输入“ 0123456789”,则该长度将为10而不是9,因为它以“ 0”开头。 Also, I wanted to ensure that if the user entered 10 letters, this would be declined because only numbers are allowed. 另外,我想确保如果用户输入10个字母,由于只允许输入数字,因此将拒绝该字母。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

At no point in your code do you actually check that the input is an integer.. You could do something like this: 实际上,您在代码中都没有检查输入是否为整数。您可以执行以下操作:

while True:
    number = input("Enter number: ")
    if not number.isdigit(): # check if a string contains a number with .isdigit()
        print ("Enter only numbers\n")
        continue
    elif len(number) != 10:
        print ("Enter 10 digits\n")
        continue
    else: 
        break

Info on str.isdigit() : 有关str.isdigit()

Type:       method_descriptor
String Form:<method 'isdigit' of 'str' objects>
Namespace:  Python builtin
Docstring:
S.isdigit() -> bool

Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
while True:
    number = input("Enter number: ")
    try:
        number = int(number)
    except ValueError:
        print ("Enter only numbers")
    else:
        if 10 > number//10**9 > 0 :
            print ("Enter 10 digits")
            continue
        else: 
            break

You can checking for an error while casting to a number (integer) 您可以在强制转换为数字(整数)时检查错误

try: 
    if len(number) != 10 and int(number):
        print ("Your number is good!")
        break

    else :
        print("Your number isn't good!")
        continue

except ValueError:

    print('only digit please')
    continue

Note that the flaw in len(number) is that the nine firsts digit could be 0. 请注意,len(number)的缺陷在于,九个首位数字可能为0。

To ensure that is a valid number beetwen 1000000000 and 999999999 you can do the following: 为确保有效的数字beetwen 1000000000和999999999,您可以执行以下操作:

import math
while True:

    try: 
        if math.floor(math.log10(int(number)))+1 == 10:
            print ("Your number is good!")
            break

        else :
            print("Your number isn't good!")
            continue

    except ValueError:

        print('only digit please')
        continue

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生成 10 位密码 - Generating a 10-digit password 查找连续有七个“ 7”的所有10位质数-Python - Finding all the 10-digit prime numbers, that have seven “7” in a row - Python Pytorch MNIST 自动编码器学习 10 位分类 - Pytorch MNIST autoencoder to learn 10-digit classification 在Python中计算10位数字中的零,几率和偶数的数量? - Counting the number of zeroes, odds, and evens in a 10-digit number in Python? (e 中的第一个 10 位素数).com python google challenge 2004 - (the first 10-digit prime in e).com python google challenge 2004 如何将python日期格式转换为mysql的10位日期格式? - How to convert python date format to 10-digit date format for mysql? Python:过滤或搜索str列表时将范围应用于通配符(需要将任何没有10位数字的str列表项添加到列表中) - Python: apply a range to wildcard when filtering or searching a str list (need to add any str list item that doesn't have a 10-digit number to a list) 如何在python脚本中仅获取10位电话号码 - How to get only 10 digit phone numbers in a python script 生成所有k的数字序列,该数字序列的第k位从左到右从右到右加到10 - Generate sequence of numbers whose k-th digit from the left and from the right sums to 10 for all k 从 m 到 n 斐波那契数中查找总和的最后一位。 (0 ≤ ≤ 10^14) - Finding last digit of sum from m to n Fibonacci numbers. (0 ≤ 𝑚 ≤ 𝑛 ≤ 10^14)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM