2024-06-26 14:00:44 发布
网友
我试着从键盘上读取一个数字并验证它
这是我有的,但不管用。你知道吗
没有错误,但它不记得我介绍的号码
def IsInteger(a): try: a=int(a) return True except ValueError: return False def read(): a=input("Nr: ") while (IsInteger(a)!=True): a=input("Give a number: ") a=0 read() print(a)
我想这就是你想要达到的目标。你知道吗
def IsInteger(a): try: a=int(a) return True except ValueError: return False def read(): global a a=input("Nr: ") while (IsInteger(a)!=True): a=input("Give a number: ") a=0 read() print(a)
您需要使用global表达式来覆盖全局变量,而无需在函数内创建return并键入a = read()。你知道吗
global
return
a = read()
但是我强烈建议你使用return并重新分配'a'的值,就像下面有人说的那样。你知道吗
似乎您没有返回read()函数的结果。你知道吗
read函数的最后一行应该是“returna”
当你调用read函数时,你会说“a=read()”
a是这两个函数的局部变量,对其余代码不可见。修复代码的最佳方法是从read()函数返回a。另外,在IsInteger()函数中,间距是关闭的。你知道吗
a
read()
IsInteger()
def IsInteger(b): try: b=int(b) return True except ValueError: return False def read(): a=input("Nr: ") while not IsInteger(a): a=input("Give a number: ") return a c = read() print(c)
我想这就是你想要达到的目标。你知道吗
您需要使用
global
表达式来覆盖全局变量,而无需在函数内创建return
并键入a = read()
。你知道吗但是我强烈建议你使用
return
并重新分配'a'的值,就像下面有人说的那样。你知道吗似乎您没有返回read()函数的结果。你知道吗
read函数的最后一行应该是“returna”
当你调用read函数时,你会说“a=read()”
a
是这两个函数的局部变量,对其余代码不可见。修复代码的最佳方法是从read()
函数返回a
。另外,在IsInteger()
函数中,间距是关闭的。你知道吗相关问题 更多 >
编程相关推荐