将数字添加到寄存器中 [英] adding numbers into a register

查看:138
本文介绍了将数字添加到寄存器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图简单地添加一个用户输入的整数,并将其放入寄存器。

我试图检查用户输入的内容实际上是一个整数在将它添加到列表(寄存器)之前还有11个数字。



 register = [] 

def add_number(number):
if isinstance(number,int) len(number)== 11
register.append(number)

add_number(raw_input( 在寄存器中添加一个数字))

print 注册





我用谷歌搜索了这个并尝试了很多方法来做到这一点使用类似的东西检查它是否是一个整数或首先将输入转换为字符串然后检查该字符串是否匹配1234567890但是我很困惑为什么这不起作用。我有一个类似于这个的功能,但只有名称而不是所以我不明白为什么这不会工作。

解决方案

嗯,这是工作只是罚款:

 register = [] 

def add_number(number):
n = str(number)
if n.isdigit() len( n)== 11
register.append(number)

add_number( 12345678901
add_number( 12345678902

print 注册





请注意, 1234567890 仅包含10位数字。


I am trying to simply add a integer that the user types in, and put that into a register.
I am trying to check that what the user has typed in is in fact a integer and also 11 numbers long before adding it to a list (register).

register = []

def add_number(number):
    if isinstance(number, int) and len(number) == 11:
        register.append(number)
        
add_number(raw_input("add a number to the register"))

print register



I have googled this and tried many ways to do this using things like type to check if it is a integer or converting the input into a string first then checking if that string matches 1234567890 but am confused why this wont work. I have got a function to work similar to this one but only with names instead so i can't understand why this wont work.

解决方案

Well, this is working just fine:

register = []
 
def add_number(number):
    n = str(number)
    if n.isdigit() and len(n) == 11:
        register.append(number)
        
add_number(12345678901)
add_number(12345678902)
 
print register



Still, please note, that 1234567890 contains only 10 digits.


这篇关于将数字添加到寄存器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆