在下面的代码中,我试图使用python脚本创建一个"更多"命令(unix),方法是将文件读入列表并一次打印10行,然后询问用户是否要打印下10行(打印更多. ).问题是raw_input一次又一次地要求输入,如果我给'y'或'Y'作为输入并且不继续while循环并且如果我给任何其他输入while循环制动.我的代码可能不是最好的学习python.
import sys
import string
lines = open('/Users/abc/testfile.txt').readlines()
chunk = 10
start = 0
while 1:
block = lines[start:chunk]
for i in block:
print i
if raw_input('Print More..') not in ['y', 'Y']:
break
start = start + chunk
Run Code Online (Sandbox Code Playgroud)
我得到的输出代码是: -
--
10 lines from file
Print More..y
Print More..y
Print More..y
Print More..a
Run Code Online (Sandbox Code Playgroud) 所以,我正在使用PHP构建我的第一个超级基本CMS.我不想简单地复制我正在观看的教程中的代码,但我真的很了解它.困扰我的一件事是使用while循环来获取帖子.在下面的代码中,我无法看到圆括号内的语句如何构成条件.在我看来它所做的就是为变量$ post分配一个数组.我想,你怎么能循环不是条件的东西是我的问题.谢谢!
function get_posts () {
$query = mysql_query("SELECT * FROM posts") or die(mysql_error());
while ($post = mysql_fetch_assoc($query)) {
echo $post['Content'];
}
}
Run Code Online (Sandbox Code Playgroud) php mysql while-loop content-management-system conditional-statements
我已经阅读了很多关于while循环的主题,我找不到一个告诉我自己的代码错误.我正在努力学习Python,我编写了这段代码,以满足练习练习33的练习练习#1.我无法弄清楚当我输入原始数据时循环不会终止的原因.
numbers = []
def number_uno(z):
i = 0
while i < z:
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
print "Pick a random number: "
z = raw_input("> ")
number_uno(z)
print "Done"
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?它只是不断向"i"添加1并且不会停止打印.
谢谢,扎克
根据谷歌C++样式指南,"当忽略返回值时,'pre'形式(++i)的效率永远不会低于'post'形式(i++),并且效率通常更高."
该指南继续解释原因,但我不完全理解.思考?也许有人可以提供这个概念的例子?
我是python的新手,我在查看为什么这段代码不起作用时遇到了问题.我希望它返回[10,122,2].
close = [5000,5010,5132,5134]
def difference():
x = 0
data = []
while x < len(close):
diff = close[x+1]-close[x]
data.append(diff)
x = x + 1
return data
Run Code Online (Sandbox Code Playgroud)
它返回"IndexError:list index out of range"但我的理解是while循环仅在满足条件时运行.我错过了什么?谢谢
我在我的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) 我希望能够在我的程序中实现一个条件,它只能运行N个小时,也许用户可以指定N,但是我们不能超前.
我想我可以使用datetime.now() 并将下面的值存储在变量中,time
>>> time
>>> time = datetime.datetime(2013, 12, 9, 21, 50, 32, 405329)
Run Code Online (Sandbox Code Playgroud)
关于如何访问( - )之间的第四个字段,看到它是一个字符串的任何想法?
我的条件就像while time != timeEndtimeEnd将是下面的值:
>>> timeEnd = datetime.datetime(2013, 12, 9, 21+N, 50, 32, 405329)
谢谢
我正在尝试使用数组while read,但整个数组立即输出.
#!/bin/bash
declare -a arr=('1:one' '2:two' '3:three');
while read -e it ; do
echo $it
done <<< ${arr[@]}
Run Code Online (Sandbox Code Playgroud)
它应该单独输出每个值(但不是),所以也许读取不是热门票这里?
Python中while/else的Java等价物是什么?因为它在Java中不起作用.第一部分是我的python代码,第二部分是我尝试将其翻译成Java.编辑:tring复制while-else
while temp.frontIsClear():
if temp.nextToABeeper():
temp.pickBeeper()
count += 1
temp.move()
else:
if temp.nextToABeeper():
temp.pickBeeper()
count += 1
print "The count is ", count
Run Code Online (Sandbox Code Playgroud)
Java尝试
Robot temp = new Robot();
int count = 0;
while (temp.frontIsClear())
{
if (temp.nextToABeeper())
{
temp.pickBeeper();
count += 1;
}
temp.move();
}
else
{
if (temp.nextToABeeper())
{
temp.pickBeeper();
count += 1;
}
}
print ("The count is ", count);
Run Code Online (Sandbox Code Playgroud) 所以这就是我正在努力解决的问题.我希望Yes和No两者都相同,并且结束循环(我还没有开始),但是当输入的内容不是Yes或No时它会再次循环问题,直到是或否输入.我无法弄清楚如何做这个部分,我还在学习,而循环对我来说是个新的基础.
我正在使用2.7
d1 = raw_input('Please answer with Yes or No.\n')
if d1 == 'Yes':
print('Good, let\'s begin.')
elif d1 == 'No':
print('Good, let\'s begin.')
if d1 == 'yes':
print('Good, let\'s begin.')
elif d1 == 'no':
print('Good, let\'s begin.')
else:
ps('Mmmmm...')
Run Code Online (Sandbox Code Playgroud)