Python 解决“Goto”问题

发布于 2024-10-20 09:28:22 字数 330 浏览 8 评论 0原文

目前我要检查字符串中是否包含特定字符。

我正在尝试寻找“Goto”功能的解决方法。

这就是我现在所拥有的:

chars = set('0123456789$,')

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

如果条目无效,我只需要 Python 返回到“UserInputAmount”的字符串输入。如果能朝正确的方向推动,我们将不胜感激。

At the moment I have a check to see if a string has specific characters in it.

I am trying to find a work around for a 'Goto' function.

This is what I have at the moment:

chars = set('0123456789$,')

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

I just need Python to go back to the string input of 'UserInputAmount' if the entry is invalid. A push in the right direction would be appreciate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一影成城 2024-10-27 09:28:22

你不需要 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

You don't need a goto, you just need a loop. Try this, which loops forever unless the user provides valid input:

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
如梦 2024-10-27 09:28:22

当我学习 Pascal 时,我们使用了一种称为“启动阅读”的小技巧:

chars = set('0123456789$,')

UserInputAmount = raw_input("Enter something: ")
while not any((c in chars) for c in UserInputAmount):
    UserInputAmount = raw_input("Wrong! Enter something else: ")
print "Input accepted."

A little technique we used to call a "priming read" back when I learned Pascal:

chars = set('0123456789$,')

UserInputAmount = raw_input("Enter something: ")
while not any((c in chars) for c in UserInputAmount):
    UserInputAmount = raw_input("Wrong! Enter something else: ")
print "Input accepted."
深海里的那抹蓝 2024-10-27 09:28:22

重复本的:


>>> chars = set('1234567')
>>> while not any((c in chars) for c in raw_input()):
...  print 'try again'
... else:
...  print 'accepted'
... 
abc
try again
123
accepted

riffing on Ben's:


>>> chars = set('1234567')
>>> while not any((c in chars) for c in raw_input()):
...  print 'try again'
... else:
...  print 'accepted'
... 
abc
try again
123
accepted
套路撩心 2024-10-27 09:28:22
goodEntry = False
first = True
chars = frozenset("abc")  #whatever
validateEntry = lambda x: any( (c in chars) for c in inString)

while not goodEntry:
    if not first: print "Invalid input"
    first = False
    print "Enter input: "
    inString = raw_input()
    goodEntry = validateEntry(inString)
goodEntry = False
first = True
chars = frozenset("abc")  #whatever
validateEntry = lambda x: any( (c in chars) for c in inString)

while not goodEntry:
    if not first: print "Invalid input"
    first = False
    print "Enter input: "
    inString = raw_input()
    goodEntry = validateEntry(inString)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文