我有以下代码:
def check(onen,twon,threen,fourn,fiven):
while ((onen != twon) and (onen != threen) and (onen != fourn) and (onen != fiven)):
return onen
else:
onen = random.randint(1,45)
我想问一下如何做到这一点:
def check(onen,twon,threen,fourn,fiven):
while ((onen != twon) and (onen != threen) and (onen != fourn) and (onen != fiven)):
return onen
else:
onen = random.randint(1,45)
(check the condition on while again)
我想做这个循环:如果条件为假,检查并再次检查,直到它为真。
答案 0 :(得分:1)
你基本上寻找的是一个do-while循环。 Python没有do-while循环,但您可以轻松模拟一个:
def something():
while True:
# ...
# perform some task
if [condition]:
return [result]
所以在这里你必须填写[condition]
来检查结果是否令人满意,[result]
是你想要返回的。只要不满足条件,Python就会进行另一个循环。
示例强>:
假设您要查询用户输入,可以使用以下命令执行此操作:
def something():
while True:
try:
x = int(input('Enter a number'))
except ValueError:
x = None
if x is not None:
return x
所以在这里我们将继续查询一个数字,直到它是一个有效数字。
当然,我们有时可以一起折叠任务和条件检查。在这里,我们可以将上述程序转换为:
def something():
while True:
try:
return int(input('Enter a number'))
except ValueError:
pass
答案 1 :(得分:1)
好像你倒退了。试试这个:
jQuery(function($){
var $content = $('.projects');
var $loader = $('#more_posts');
var ppp = 4;
var offset = $('.projects').find('.project').length;
$loader.on( 'click', load_ajax_posts );
function load_ajax_posts() {
if ( !($loader.hasClass('post_loading_loader') || $loader.hasClass('post_no_more_posts')) ) {
$.ajax({
type: 'POST',
dataType: 'html',
url: screenReaderText.ajaxurl,
data: {
'ppp': ppp,
'offset': offset,
'action': 'mytheme_more_post_ajax'
},
beforeSend : function () {
$loader.addClass('post_loading_loader').html( screenReaderText.loading );
},
success: function (data) {
var $data = $(data);
if ($data.length) {
var $newElements = $data.css({ opacity: 0 });
$content.append($newElements);
$content.isotope( 'appended', $newElements );
$content.isotope( 'reloadItems' ); // https://isotope.metafizzy.co/methods.html#reloaditems
$content.isotope('layout'); // https://isotope.metafizzy.co/methods.html#layout
$newElements.animate({ opacity: 1 });
$loader.removeClass('post_loading_loader').html(screenReaderText.loadmore);
} else {
$loader.removeClass('post_loading_loader').addClass('post_no_more_posts').html(screenReaderText.noposts);
}
},
error : function (jqXHR, textStatus, errorThrown) {
$loader.html($.parseJSON(jqXHR.responseText) + ' :: ' + textStatus + ' :: ' + errorThrown);
console.log(jqXHR);
},
});
}
offset += ppp;
return false;
}
});
对于您的具体示例:
while not condition:
change condition
return that
或更短:
def check(onen, twon, threen, fourn, fiven):
while not ((onen != twon) and (onen != threen) and (onen != fourn) and (onen != fiven)):
onen = random.randint(1,45)
return onen
或多更短,没有循环(但只适用于小范围):
def check(onen, twon, threen, fourn, fiven):
while onen in (twon, threen, fourn, fiven):
onen = random.randint(1,45)
return onen
但请注意,这些都不会改变函数之外的def check(onen, twon, threen, fourn, fiven):
return random.choice([x for x in range(1, 46)
if x not in (twon, threen, fourn, fiven)])
的值(当然,除非你做onen
)。
答案 2 :(得分:0)
从更新到你的问题,似乎你只需要反思时间的感觉来得到你想要的东西:
def condition(onen, twon, threen, fourn, fiven):
return ((onen != twon) and (onen != threen) and (onen != fourn) and (onen != fiven))
def check(onen, twon, threen, fourn, fiven):
while not condition(onen, twon, threen, fourn, fiven):
onen = random.randint(1,45)
return onen
答案 3 :(得分:0)
你也可以试试这个:
_Pragma("prolog(foo, \"#MCPROLG MAIN=(YES,16,132)\"")
答案 4 :(得分:-1)
def something():
while(condition):
return that
else:
return this
something() # just callback the function
你也可以删除else语句并只回调当前函数