如何检查输入是否为整数,同时使用 FOR for RAW_INPUT ..in Python?
How can I check input is integer, while using FOR for RAW_INPUT ..in Python?
我有任务
Print the minimal value from list you entered. First entered value
defines the length of the list. Each next value should be placed in
list one by one. Use operator for.
n
是之前定义的,通过输入和 start_n = 1
def list2_func():
global list2
list2 = []
for i in xrange(start_n, n + 1):
list2.append(raw_input('Enter the %s number: ' % i))
list2_check()
def list2_check():
global start_n
try:
value = int(list2[-1])
except ValueError:
print "Please use only 0-9 keys. Re enter %s number" % len(list2)
start_n = len(list2)
list2_func()
else:
start_n = start_n + 1
每次我输入任何未通过 try
的键时,它都会再次要求输入相同的值 - 这很棒。但是当我输入我的最后一个值(例如 n = 4
,所以第四个值)程序再次要求我输入。最后我得到了 2*n - 1
个值——这不是我想要的。
能否建议我使用其他方法来检查输入的值是否为数字?或者指出我代码中的错误!
我正在使用 python 2.7.
您的代码中可能存在错误。如果检查失败,您将多次附加该号码。请尝试以下代码,以确保您将单个有效值附加到列表中。
def get_value(i):
while True:
number = raw_input('Enter the %s number: ' % i)
try:
value = int(number)
except:
continue
return value
def list2_func():
list2 = []
for i in xrange(start_n, n + 1):
number = get_value(i)
list2.append(number)
您不需要使用 list2_check 功能:
def list2_func():
list2 = []
i=start_n
while i<n+1:
try:
list2.append(int(raw_input('Enter the %s number: ' % i)))
i+=1
except ValueError:
print "Please use only 0-9 keys. Re enter %s number" % len(list2)
return list2
我还删除了您的全局变量,因为使用 return 比使用全局变量更可取。 (如果您尝试使用另一个同名变量,它们可能会导致问题)
刚玩了一下代码就发现了问题。程序首先完成 FOR 循环并为每个未通过 try 测试的值开始新的 for 循环.
def list2_func():
global list2
list2 = []
for i in xrange(1, n+1):
list2.append(raw_input('Enter the %s number: ' % i))
list2_check()
def list2_check():
try:
value = int(list2[-1])
except ValueError:
list2[-1]= raw_input("Please, use only 0-9 keys" % len(list2))
list2_check()
else:
pass
现在它只是要求替换错误的值而不是再次开始 for 循环:)
这是因为你有一个递归函数。每个函数调用另一个。
此外,strart_n 在每个循环中被修改(删除 try 的其他情况)。
您的问题出在所使用的逻辑上:您添加了错误的输入值(通过 list2.append),然后检查列表,如果值错误则永远不要删除最后输入的值。
所以,如果你想保留你的代码,你只需要在引发 ValueError 时删除列表中的最后一项。
但是,您的代码还有许多其他问题需要解决:
你使用递归,很容易使你的程序崩溃:只输入错误的值:每次,你在 "list2_func" 中对 "list2_func" 进行新的调用。因此,对于 5 个连续的错误值,我们有:
call to list2_func:
call to list2_func:
call to list2_func:
call to list2_func:
call to list2_func:
当然,python 会在达到最大递归时崩溃:)
另一个问题是全局变量的使用。这不好。只有当你真的需要一个全局变量时才这样做。
这是您的练习现有的众多解决方案之一:
def get_list():
"""
Returns an user's entered list.
First entered value defines the length of the list.
Each next value should be placed in list one by one.
"""
wanted_length = None
my_list = []
while wanted_length < 1:
try:
wanted_length = int(raw_input('Please enter the wanted length of the list (int > 0) : '))
except ValueError:
pass
i = 1
while i <= wanted_length:
try:
my_list.append(int(raw_input('Please enther the number %d (int only) : ' % i)))
except ValueError:
pass
else:
i += 1
return my_list
def print_min_with_for(l):
"""
Prints the minimal value from list you entered using operator `for`.
"""
if not l:
print 'List is empty'
return
min_value = None
for i in l:
if min_value is None or i < min_value:
min_value = i
print 'min value in the list is : %d' % min_value
my_list = get_list()
print 'Printed min value in the list should be : %d' % min(my_list)
print_min_with_for(my_list)
欢迎来到 python 顺便说一句 ;-)
我有任务
Print the minimal value from list you entered. First entered value defines the length of the list. Each next value should be placed in list one by one. Use operator for.
n
是之前定义的,通过输入和 start_n = 1
def list2_func():
global list2
list2 = []
for i in xrange(start_n, n + 1):
list2.append(raw_input('Enter the %s number: ' % i))
list2_check()
def list2_check():
global start_n
try:
value = int(list2[-1])
except ValueError:
print "Please use only 0-9 keys. Re enter %s number" % len(list2)
start_n = len(list2)
list2_func()
else:
start_n = start_n + 1
每次我输入任何未通过 try
的键时,它都会再次要求输入相同的值 - 这很棒。但是当我输入我的最后一个值(例如 n = 4
,所以第四个值)程序再次要求我输入。最后我得到了 2*n - 1
个值——这不是我想要的。
能否建议我使用其他方法来检查输入的值是否为数字?或者指出我代码中的错误!
我正在使用 python 2.7.
您的代码中可能存在错误。如果检查失败,您将多次附加该号码。请尝试以下代码,以确保您将单个有效值附加到列表中。
def get_value(i):
while True:
number = raw_input('Enter the %s number: ' % i)
try:
value = int(number)
except:
continue
return value
def list2_func():
list2 = []
for i in xrange(start_n, n + 1):
number = get_value(i)
list2.append(number)
您不需要使用 list2_check 功能:
def list2_func():
list2 = []
i=start_n
while i<n+1:
try:
list2.append(int(raw_input('Enter the %s number: ' % i)))
i+=1
except ValueError:
print "Please use only 0-9 keys. Re enter %s number" % len(list2)
return list2
我还删除了您的全局变量,因为使用 return 比使用全局变量更可取。 (如果您尝试使用另一个同名变量,它们可能会导致问题)
刚玩了一下代码就发现了问题。程序首先完成 FOR 循环并为每个未通过 try 测试的值开始新的 for 循环.
def list2_func():
global list2
list2 = []
for i in xrange(1, n+1):
list2.append(raw_input('Enter the %s number: ' % i))
list2_check()
def list2_check():
try:
value = int(list2[-1])
except ValueError:
list2[-1]= raw_input("Please, use only 0-9 keys" % len(list2))
list2_check()
else:
pass
现在它只是要求替换错误的值而不是再次开始 for 循环:)
这是因为你有一个递归函数。每个函数调用另一个。 此外,strart_n 在每个循环中被修改(删除 try 的其他情况)。
您的问题出在所使用的逻辑上:您添加了错误的输入值(通过 list2.append),然后检查列表,如果值错误则永远不要删除最后输入的值。 所以,如果你想保留你的代码,你只需要在引发 ValueError 时删除列表中的最后一项。
但是,您的代码还有许多其他问题需要解决: 你使用递归,很容易使你的程序崩溃:只输入错误的值:每次,你在 "list2_func" 中对 "list2_func" 进行新的调用。因此,对于 5 个连续的错误值,我们有:
call to list2_func:
call to list2_func:
call to list2_func:
call to list2_func:
call to list2_func:
当然,python 会在达到最大递归时崩溃:)
另一个问题是全局变量的使用。这不好。只有当你真的需要一个全局变量时才这样做。
这是您的练习现有的众多解决方案之一:
def get_list():
"""
Returns an user's entered list.
First entered value defines the length of the list.
Each next value should be placed in list one by one.
"""
wanted_length = None
my_list = []
while wanted_length < 1:
try:
wanted_length = int(raw_input('Please enter the wanted length of the list (int > 0) : '))
except ValueError:
pass
i = 1
while i <= wanted_length:
try:
my_list.append(int(raw_input('Please enther the number %d (int only) : ' % i)))
except ValueError:
pass
else:
i += 1
return my_list
def print_min_with_for(l):
"""
Prints the minimal value from list you entered using operator `for`.
"""
if not l:
print 'List is empty'
return
min_value = None
for i in l:
if min_value is None or i < min_value:
min_value = i
print 'min value in the list is : %d' % min_value
my_list = get_list()
print 'Printed min value in the list should be : %d' % min(my_list)
print_min_with_for(my_list)
欢迎来到 python 顺便说一句 ;-)