Python为'转到'工作

The*_*Woo 0 python goto

目前,我检查字符串中是否包含特定字符.

我试图找到一个'Goto'功能的解决方案.

这就是我现在所拥有的:

chars = set('0123456789$,')

if any((c in chars) for c in UserInputAmount):
    print 'Input accepted'
else:
    print 'Invalid entry. Please try again'
Run Code Online (Sandbox Code Playgroud)

如果条目无效,我只需要Python返回'UserInputAmount'的字符串输入.推动正确的方向将是欣赏.

Ben*_*ank 6

你不需要goto,你只需要一个循环.试试这个,除非用户提供有效的输入,否则它会永远循环:

chars = set('0123456789$,')

while True: # loop "forever"
    UserInputAmount = raw_input() # get input from user

    if any((c in chars) for c in UserInputAmount):
        print 'Input accepted'
        break # exit loop

    else:
        print 'Invalid entry. Please try again'
        # input wasn't valid, go 'round the loop again
Run Code Online (Sandbox Code Playgroud)