我只是想创建一个要求正整数的函数,然后验证输入确实是正整数:
def int_input(x):
x = input('Please enter a positive integer:')
if x != type(int()) and x < 1:
print("This is not a positive integer, try again:")
else:
print(x)
int_input(x)
它给我“NameError:名称'x'未定义”。
这太简单了,我觉得我应该找到很多关于这方面的帖子,所以也许我是盲目的......
谢谢!
最佳答案
def int_input():
x = input('Please enter a positive integer:')
if x != type(int()) and x < 1:
print("This is not a positive integer, try again:")
else:
print(x)
int_input()
应该是这样的,不声明x
就不能调用函数int_input()
关于python - 要求正整数并验证输入的简单函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40769595/