我在我的PHP代码中有这个While循环,我在一个div中回显行,就像一个按钮,当我按下"按钮"时,我想隐藏/显示连接的div与jQuery函数"toggle".我只是让它工作,所以当我点击任何"按钮"时,它会打开所有div,而不仅仅是那个已连接的div.
<script>
$(document).ready(function(){
$(".scorec").click(function(){
$(".scorematcho").slideToggle('slow');
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
那是我目前正在使用的jQuery.
<?php $RM = mysql_query("SELECT * FROM score ORDER BY ScoreID DESC LIMIT 3");
while ($ScoreD = mysql_fetch_assoc($RM)) {
$a = $ScoreD["ScoreOne"]; $b = $ScoreD["ScoreTwo"];?>
<div class="scorec" >
<?php if($a>$b){;?><font color="green"><?php }else{?><font color="red"><?php }?>
<div class="scorel"><img src="flags/<?php echo $ScoreD["FlagOne"]; ?>.png" style="float:left;">
<?php echo $ScoreD["TeamOne"]; ?> | <?php echo $ScoreD["ScoreOne"]; ?></div>
<div class="scorem">-</div>
<div class="scorer"><?php echo $ScoreD["ScoreTwo"]; ?> | <?php echo $ScoreD["TeamTwo"]; ?>
<img src="flags/<?php echo $ScoreD["FlagTwo"]; ?>.png" style="float:right;"></div></font>
</div>
<div class="scorematcho" > …Run Code Online (Sandbox Code Playgroud) 我不是一个经验丰富的程序员,我的代码有问题,我认为这是我的逻辑错误,但我无法在http://anh.cs.luc.edu/python/hands-on/找到答案3.1/handsonHtml/whilestatements.html.我想要的是检查串行设备是否被锁定,并且"它被锁定"和"它未被锁定"的条件之间的不同之处,,,,在于该行中有4个逗号包含GPGGA字母.所以我希望我的代码启动,如果没有,,,,,但我想我的循环是错误的.任何建议将不胜感激.提前致谢.
import serial
import time
import subprocess
file = open("/home/pi/allofthedatacollected.csv", "w") #"w" will be "a" later
file.write('\n')
while True:
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
checking = ser.readline();
if checking.find(",,,,"):
print "not locked yet"
True
else:
False
print "locked and loaded"
Run Code Online (Sandbox Code Playgroud)
...
基本上我想做的是从std输入中读取一个变量,并检查该变量是否为1或2。如果不是,我想通知用户并再次询问,我认为while循环是有意义的(我正在使用bash外壳中的OS X小牛)
对于单个条件,此方法有效:
read result
while [[ "$result" != "1" ]];
do
echo $result
echo "You must enter 1 or 2. Please try again: "
read result
done
echo "success"
Run Code Online (Sandbox Code Playgroud)
但是当我尝试执行或条件时:
while [[ "$result" != "1" ]] || [[ "$result" != "2" ]];
Run Code Online (Sandbox Code Playgroud)
如果我在终端输入1或2,这与输入1或2不匹配。
while [[ "$result" != "1" ] || [ "$result" != "2" ]];
Run Code Online (Sandbox Code Playgroud)
产生语法错误。
while [[ "$result" != "1" || "$result" != "2" ]];
Run Code Online (Sandbox Code Playgroud)
这也不匹配1或2。有谁知道正确的语法?
考虑这个脚本
#!awk -f
BEGIN {
"date" | getline foo
print foo
}
Run Code Online (Sandbox Code Playgroud)
它将按预期打印当前日期.但是如果你把它放在一个循环中
#!awk -f
BEGIN {
while (1) {
"date" | getline foo
printf "%s\r", foo
}
}
Run Code Online (Sandbox Code Playgroud)
它只是重复打印相同的日期.我想用一个循环来捕捉外部命令两种getline或system
我有一个由while循环组成的函数.在while循环中,我调用了许多私有方法.如果任何方法失败(返回false,但也抛出私有设计的异常),我想继续下一次迭代.
例:
void func (){
while (true){
func1();
func2();
func3();
}
}
Run Code Online (Sandbox Code Playgroud)
正如我所说,每个func也会在出错时抛出myException对象.
谢谢!
Python新手在这里很抱歉我确定这是一个愚蠢的问题,但我似乎无法在一个教程中解决以下挑战,该教程要求我使用while循环来检查有效的用户输入.
(使用Python2.7)
这是我的代码,但它无法正常工作:
choice = raw_input('Enjoying the course? (y/n)')
student_surveyPromptOn = True
while student_surveyPromptOn:
if choice != raw_input('Enjoying the course? (y/n)'):
print("Sorry, I didn't catch that. Enter again: ")
else:
student_surveyPromptOn = False
Run Code Online (Sandbox Code Playgroud)
以上打印到控制台:
Enjoying the course? (y/n) y
Enjoying the course? (y/n) n
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) x
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n)
Run Code Online (Sandbox Code Playgroud)
这显然是不正确的 - 当用户输入'y'或'n'时循环应该结束但我不知道如何做到这一点.我在这做错了什么?
注意:挑战要求我同时使用!=运算符和loop_condition
在Ruby中,循环似乎与彼此完全相同,while和until循环.
使用一个而不是另一个会有什么情况,为什么Ruby有两个似乎做同样事情的循环?
while循环语法如下:
while conditional [do]
code
end
Run Code Online (Sandbox Code Playgroud)
而直到语法:
until conditional [do]
code
end
Run Code Online (Sandbox Code Playgroud)
所以要尽可能清楚:
$i = 0
$num = 5
while $i < $num do
puts("Inside the loop i = #$i" )
$i +=1
end
Run Code Online (Sandbox Code Playgroud)
和
$i = 0
$num = 5
until $i < $num do
puts("Inside the loop i = #$i" )
$i +=1;
end
Run Code Online (Sandbox Code Playgroud)
将产生两个相同的输出:
Inside the loop i = 0
Inside the loop i = 1
Inside the loop …Run Code Online (Sandbox Code Playgroud) 我最近开始学习Java并在测试时发现了一个问题.这可能是一个非常简单的问题,但我似乎无法解决它.这是我的代码:
int firstj = 1;
if (firstj == 1) {
String choice = "Type a number between 1 and 4";
System.out.println(choice);
while (true) {
if (firstj == 1) {
Scanner third = new Scanner(System.in);
String thirdch = third.nextLine();
while (true) {
if (thirdch.equals("1")) {
System.out.println("Show choice and accept input again ");
System.out.println(choice);
break;
} else if (thirdch.equals("2")) {
System.out.println("Show choice and accept input again ");
System.out.println(choice);
break;
} else if (thirdch.equals("3")) {
System.out.println("Show choice and accept input again ");
System.out.println(choice); …Run Code Online (Sandbox Code Playgroud) 我正在调用一个函数,如果尚未加载网页,它将引发异常。我想等待2秒钟,然后重试,直到页面加载完毕。
我尝试这样:
while(True):
try:
some_funciont()
break
except:
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)
但它在第一次迭代后会逸出。
如果未引发异常,该如何逃生?
我正在尝试创建一个等于列表中每个其他数字之和的函数.例如,如果列表为[0,1,2,3,4,5],则函数应等于5 + 3 + 1.我怎么能这样做?我对Python的了解并没有比while和for循环更远.谢谢.