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'
如果条目无效,我只需要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不需要 goto,你只需要一个循环。试试这个,除非用户提供有效的输入,否则它将永远循环:
You don't need a goto, you just need a loop. Try this, which loops forever unless the user provides valid input:
当我学习 Pascal 时,我们使用了一种称为“启动阅读”的小技巧:
A little technique we used to call a "priming read" back when I learned Pascal:
重复本的:
riffing on Ben's: