我想添加我的ul&li每两个循环..例子
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<ul>
<li> <?php the_title() ?> - <?php the_content() ?></li>
</ul>
<?php endwhile; ?>
Run Code Online (Sandbox Code Playgroud)
假设我有4个帖子,我希望结果应该是这样的
<ul>
<li>Title 1 - content 1</li>
<li>Title 2 - content 2</li>
</ul>
<ul>
<li>Title 3 - content 3</li>
<li>Title 4 - content 4</li>
</ul>
Run Code Online (Sandbox Code Playgroud) 我现在正在尝试自学python,而我正在使用"学习Python艰难之路"中的练习来完成这项工作.
现在,我正在进行涉及while循环的练习,在这里我从脚本中获取while循环,将其转换为函数,然后在另一个脚本中调用该函数.最终程序的唯一目的是将项添加到列表中,然后在列表中打印.
我的问题是,一旦我调用该函数,嵌入式循环决定无限继续.
我已经多次分析了我的代码(见下文),并且找不到任何明显的错误.
def append_numbers(counter):
i = 0
numbers = []
while i < counter:
print "At the top i is %d" % i
numbers.append(i)
i += 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
count = raw_input("Enter number of cycles: ")
print count
raw_input()
append_numbers(count)
Run Code Online (Sandbox Code Playgroud) 在JavaScript中,我最近遇到了反向循环更快的问题.
我以这种形式看过它们:
var i = someArray.length;
while (i--) {
console.log(someArray[i]);
}
Run Code Online (Sandbox Code Playgroud)
我测试了它,一旦它通过整个阵列就停止了.
一旦完成阵列,它如何知道何时停止?
我是Perl的100%新手,但确实有一些PHP知识.我正在尝试创建一个快速脚本,它将获取@url变量并将其保存为.txt文件.我遇到的问题是它每次运行循环时都会再次保存url,这非常烦人.所以当循环运行时,它看起来像这样.
url1.com
url1.com url2.com
url1.com url2.com url3.com
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像一个简单明了的:
url1.com
url2.com
url3.com
Run Code Online (Sandbox Code Playgroud)
这是我的代码.如果有人可以提供帮助,我会非常感激!
#!/usr/bin/perl
use strict;
use warnings;
my $file = "data.rdf.u8";
my @urls;
open(my $fh, "<", $file) or die "Unable to open $file\n";
while (my $line = <$fh>) {
if ($line =~ m/<(?:ExternalPage about|link r:resource)="([^\"]+)"\/?>/) {
push @urls, $1;
}
open (FH, ">>my_urls.txt") or die "$!";
print FH "@urls ";
close(FH);
}
close $fh;
Run Code Online (Sandbox Code Playgroud) [社区编辑提供可重复的示例:]
def main():
e = None
print(locals())
while not e:
try:
raise Exception
except Exception as e:
pass
main()
Run Code Online (Sandbox Code Playgroud)
产生
~/coding$ python3.3 quiz2.py
{'e': None}
Traceback (most recent call last):
File "quiz2.py", line 11, in <module>
main()
File "quiz2.py", line 5, in main
while not e:
UnboundLocalError: local variable 'e' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
[已编辑]包含可重现的代码
我正在尝试运行while循环,我使用的条件是循环在变量时继续e==None.相关代码如下:
print("\nThe current score list contains the following people's scores: ")
score_list = open("score_list.dat", "rb")
score_name = []
e = None
while not e: …Run Code Online (Sandbox Code Playgroud) 我只是想从我的数据库中显示一个基本列表或项目,但由于某种原因它没有显示第一个项目,所以如果一个类别中只有一个东西它没有显示任何东西但是如果有2它将显示一个项目.我已添加以下代码.
询问
//this query will fetch 4 records only!
$latestVideos = mysql_query("SELECT * FROM table ORDER BY date DESC LIMIT 5")or die(mysql_error());
$posts = mysql_fetch_array($latestVideos);
Run Code Online (Sandbox Code Playgroud)
循环
while($posts = mysql_fetch_array($latestVideos)){
$dateFormat= $posts['video_date'];
$newDate=date('d-m-Y',strtotime($dateFormat));
echo $posts['video_title']
echo $newDate;
echo $posts['video_embed'];
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个craps()不带参数的函数,模拟一个掷骰子游戏,1如果玩家赢了以及0玩家输了则返回.
游戏规则:游戏开始时玩家投掷一对骰子.如果玩家总共掷出7或11,则玩家获胜.如果玩家总共掷出2,3或12,则玩家输了.对于所有其他掷骰价值,游戏继续进行,直到玩家滚动初始值agaian(在这种情况下玩家获胜)或7(玩家输掉).
我想我越来越近了,但我还没有,我不认为我的while循环工作正常.这是我到目前为止的代码:
def craps():
dice = random.randrange(1,7) + random.randrange(1,7)
if dice in (7,11):
return 1
if dice in (2,3,12):
return 0
newRoll = craps()
while newRoll not in (7,dice):
if newRoll == dice:
return 1
if newRoll == 7:
return 0
Run Code Online (Sandbox Code Playgroud)
如何修复while循环?我真的找不到它的问题,但我知道这是错误的或不完整的.
无论我把Y还是N都没关系,在我回答"更多的肉?"之后,我的程序结束了.我期待它将响应返回到循环.
#include <iostream>
using namespace std;
int main()
{
char response = 'y';
double price;
double total = 0;
while (response == 'Y' || 'y') {
cout << "Please enter price of meat: ";
cin >> price;
total += price;
cout << "More meat? (Y/N)";
cin >> response;
return response;
}
cout << "Your total is: " << total;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我对Chrome DevTools中的这个符号(< - )感到困惑

它的返回值还是控制台价值?
当我循环运行时
var i = 0;
while (i < 5) {
console.log(i);
i++;
}
Run Code Online (Sandbox Code Playgroud)
控制台日志吐出4次,最后4次在前面有一个(< - ),这是什么意思?
写作提示用户的格式输入社会安全号码的节目ddd-dd-dddd,其中d是一个数字.程序显示"Valid SSN"正确的社会安全号码或"Invalid SSN"不正确.我几乎拥有它,只有一个问题.
我不知道如何检查它是否是正确的格式.我可以输入例如:
99-999-9999
Run Code Online (Sandbox Code Playgroud)
它会说这是有效的.我如何解决这个问题,这样我才能得到"Valid SSN"它的格式ddd-dd-dddd?
这是我的代码:
def checkSSN():
ssn = ""
while not ssn:
ssn = str(input("Enter a Social Security Number in the format ddd-dd-dddd: "))
ssn = ssn.replace("-", "")
if len(ssn) != 9: # checks the number of digits
print("Invalid SSN")
else:
print("Valid SSN")
Run Code Online (Sandbox Code Playgroud) while-loop ×10
loops ×4
python ×4
javascript ×2
php ×2
python-3.x ×2
arrays ×1
c++ ×1
exception ×1
mysql ×1
perl ×1
response ×1
return ×1