Python 返回嵌套循环中的2步 def get_user_choice(): 全局用户选择 ''3用户提供他们的号码以进行验证'' 对于范围(1,4)内的i: 选项=[] **choices=input(“用户{},请输入以“,”分隔的5个数字:”。格式(i)) nums=choices.split(“,”) 尽管如此: 如果len(nums)!=5: 打印(“选择错误,必须输入以“,”分隔的5个数字”) 打破 对于以NUM为单位的nr: 如果编号为isdigit(): 如果整数(nr)25: 打印(“错误!输入5个介于1和25之间的数字”) 持续 其他: 打印(nr+“不是数字,请重试”) 持续 #我怎样才能从这里回到**????
此代码更正请求数字的逻辑并返回所选数字Python 返回嵌套循环中的2步 def get_user_choice(): 全局用户选择 ''3用户提供他们的号码以进行验证'' 对于范围(1,4)内的i: 选项=[] **choices=input(“用户{},请输入以“,”分隔的5个数字:”。格式(i)) nums=choices.split(“,”) 尽管如此: 如果len(nums)!=5: 打印(“选择错误,必须输入以“,”分隔的5个数字”) 打破 对于以NUM为单位的nr: 如果编号为isdigit(): 如果整数(nr)25: 打印(“错误!输入5个介于1和25之间的数字”) 持续 其他: 打印(nr+“不是数字,请重试”) 持续 #我怎样才能从这里回到**????,python,loops,Python,Loops,此代码更正请求数字的逻辑并返回所选数字 def get_user_choice(): global user_choice ''' 3users give their numbers to be validated''' for i in range(1, 4): choices = [] ** choices = input("User {}, please enter 5 numbers separated by ',' :".form
def get_user_choice():
global user_choice
''' 3users give their numbers to be validated'''
for i in range(1, 4):
choices = []
** choices = input("User {}, please enter 5 numbers separated by ',' :".format(i))
nums = choices.split(',')
while True:
if len(nums) != 5:
print(" Wrong choice,You have to enter 5 numbers separated by ','")
break
for nr in nums:
if nr.isdigit():
if int(nr) < 1 or int(nr) > 25:
print(" Wrong! Enter 5 Numbers between 1 and 25")
continue
else:
print(nr + " is not a number, try again")
continue
# How can i go back to ** from here????
def get_user_choice():
''3用户提供他们的号码以进行验证''
#全局用户选择--不明白为什么需要全局用户选择,所以现在被注释掉了
用户选择=[]
对于范围(1,4)内的i:
尽管如此:
choices=input(“用户{},请输入以“,”分隔的5个数字:”。格式(i))
nums=choices.split(“,”)
如果len(nums)!=5:
打印(“选择错误,必须输入以“,”分隔的5个数字”)
其他:
对于以NUM为单位的nr:
如果编号为isdigit():
如果整数(nr)<1或整数(nr)>25:
打印(“错误!输入5个介于1和25之间的数字”)
打破
其他:
打印(nr+“不是数字,请重试”)
打破
其他:
用户选择。附加(nums)
打破
返回用户选择
#获取用户选择
选项=获取用户选项()
上述代码的更清晰版本
def get_user_choice():
''' 3users give their numbers to be validated'''
#global user_choice--didn't understand why you needed a global user_choice so commented out for now
user_choices = []
for i in range(1, 4):
while True:
choices = input("User {}, please enter 5 numbers separated by ',' :".format(i))
nums = choices.split(',')
if len(nums) != 5:
print(" Wrong choice,You have to enter 5 numbers separated by ','")
else:
for nr in nums:
if nr.isdigit():
if int(nr) < 1 or int(nr) > 25:
print(" Wrong! Enter 5 Numbers between 1 and 25")
break
else:
print(nr + " is not a number, try again")
break
else:
user_choices.append(nums)
break
return user_choices
# Get user choices
choices = get_user_choice()
def get_user_choice():
''3用户提供他们的号码以进行验证''
无效的_numbers=lambda编号:列表(筛选器(lambda y:not y.isdigit(),numbers))
无效\u range=lambda number:list(筛选器)(lambda y:not(1)而不是使用continue
,使用break
。它应该从内部循环中断,返回到外部循环。From Review:请在您的帖子中添加说明和问题。请参阅:
def get_user_choice():
''' 3users give their numbers to be validated'''
invalid_numbers = lambda numbers: list(filter(lambda y: not y.isdigit(), numbers))
invalid_range = lambda numbers: list(filter(lambda y: not (1 <= int(y) <= 25), numbers))
user_choices = []
for i in range(1, 4):
while True:
choices = input("User {}, please enter 5 numbers separated by ',' :".format(i))
nums = choices.split(',')
if len(nums) != 5:
print(" Wrong choice, You have to enter 5 numbers separated by ','")
continue
p = invalid_numbers(nums)
if p:
print(*p,' are not numbers. Enter 5 numbers separaterd by "," ')
continue
p = invalid_range(nums)
if p:
print(*p, ' are out of range. Enter 5 numbers between 1 and 25')
continue
## uncomment if you wanted the number rather than strings
# nums = list(map(int, nums))
user_choices.append(nums)
break
return user_choices
choices = get_user_choice()