假设我有一个名为everythird的函数,它以列表作为参数,并返回一个包含原始列表的每个第三个元素的新列表,从索引0开始.我知道如何使用切片表示法执行此操作(返回everythird [0 :: 3] ]),但我们只能使用while循环.如果我输入每一个([1,2,3,4,5,6,7,8]),我希望它返回[1,4,7].我尝试了几种不同的方式,但我没有得到一个列表,或者我只得到一个值.如何退回清单?另外,您如何确定某些内容是修改还是不修改原始列表?
谢谢.
这是我尝试这种方式之一:every_third([1,2,3,4,5,6,7,8,9,10,11])
def everythird(l):
'''(list) -> list
Returns every third element of original list, starting at index 0'''
i = 0
while i < len(l):
print(l[i])
i += 3
Run Code Online (Sandbox Code Playgroud)
这打印
1
4
7
Run Code Online (Sandbox Code Playgroud) #include <cs50.h>
#include <stdio.h>
int main(void) {
printf("Enter your change: ");
int pennies = 0, nickels = 0, dimes = 0, quarters = 0;
float change = GetFloat();
while (change > 0) {
if (change >= 0.25) {
quarters++;
change -= 0.25;
}
else if (change >= 0.10) {
dimes++;
change -= 0.10;
}
else if (change >= 0.05) {
nickels++;
change -= 0.05;
}
else if (change >=0.01) {
pennies++;
change -= 0.01;
}
// force break
else { …Run Code Online (Sandbox Code Playgroud) 我有下面的代码,由于某种原因,while循环没有终止.
def iterator(i, f):
print len(f)
print i
while i < len(f):
if i == 0:
print "Restarted."
else:
print "iterate."
function()
return f
Run Code Online (Sandbox Code Playgroud)
打印语句不是必需的,但计数器i会被另一个函数增加,所以我想确保它不是问题所在.对于f包含4个项目的列表,它打印如下:
4, 0, Restarted.
4, 1, iterate.
4, 2, iterate.
4, 3, iterate.
4, 4, iterate.
4, 4, iterate.
etc..
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它继续进入while循环时i = 4和len(f) = 4.它应该打破循环并执行返回函数,但由于某种原因它不会.
任何人都可以解释为什么while循环在其条件变为false时不会终止?
编辑:一些代码,以更好地解释发生了什么.我也澄清了i变化function(),然后iterator随着增加而调用i.希望有道理.
f = [0,1,2,3]
i = 0
def iterator(i, f):
print i
while …Run Code Online (Sandbox Code Playgroud) 我有wp_places自定义表,我在打印数组时得到这个:
[0] => stdClass Object
(
[home_location] => 24
)
[1] => stdClass Object
(
[home_location] => 29
)
Run Code Online (Sandbox Code Playgroud)
现在我想以这种方式内爆价值(24,29),但在我的代码中我收到此错误:
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
Run Code Online (Sandbox Code Playgroud)
我的守则
$getGroupType = $_POST['parent_category'];
$result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
$bgroup = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bgroup[] = implode(',',$row);
}
echo implode(',',$bgroup);
Run Code Online (Sandbox Code Playgroud)
任何想法或建议?谢谢.
void main(){
/* This string needs to be printed without the '%' and in segments. */
char str[] = "Oct: %o Dec: %d Roman: %r";
int i = 0;
while (str[i] != '\0'){
/* When I run this nested loops, for some reason they don't stop at '\0'. */
while (str[i] != '%'){
printf("%c", str[i++]);
}
if (str[i] == '%')
i++;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个while循环,当满足字符串条件或计时器条件时,程序将跳出循环并打印出所需的输出.当打印出所需的输出时,输出将包含比较答案之前和之后的时间差.
但是循环并没有像我期望的那样运行.所以任何人都可以帮我弄清楚这段代码的问题在哪里?
以下是我的代码:
void startGame(time_t cd,int gl){
string guessWord;
time_t start, end, diff,timeLeft;
cout << "Scrambled word is " << randomizeWord(gl) << endl;
while (timeLeft != cd || guessWord.compare(originalWord) == 0)
{
start = time(0);
cout << "You have " << cd << " seconds to guess." << endl;
cout << "Enter guess : ";
cin >> guessWord;
end = time(0);
diff = end - start;
//total_time = total_time + diff;
timeLeft = cd - diff;
if(guessWord.compare(originalWord) != 0)
{ …Run Code Online (Sandbox Code Playgroud) 我需要一个脚本来保持轮询"receive_dir"目录,直到"stopfile"写入目录.
这必须运行尽管空目录.
到目前为止,我有这个,但如果receive_dir为空,没有"一元运算符预期"的文件,则会失败.救命 !!
#!/usr/bin/ksh
until [ $i = stopfile ]
do
for i in `ls receive_dir`; do
time=$(date +%m-%d-%Y-%H:%M:%S)
echo $time
echo $i;
done
done
Run Code Online (Sandbox Code Playgroud) 在完成此代码之前,我只是错误地测试了它,并意识到它不会停止:
$var = "any";
for ($i=1; $i < 2; $i++){
$var.$i = "any";
}
Run Code Online (Sandbox Code Playgroud)
为什么会产生无限循环?为什么PHP不会产生错误?
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CountingSundays {
public static void main(String args[]) {
Calendar cal = new GregorianCalendar(1901, 00, 01); // month set to 0for jan , 1= feb etc
while((cal.get(Calendar.YEAR) != 2001) && (cal.get(Calendar.MONTH) != 0) && cal.get(Calendar.DAY_OF_MONTH) != 1) { // while not 1/1/2001
System.out.print(cal.get(Calendar.MONTH));
// cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图通过一次添加一天迭代while循环,但它甚至不会第一次访问while循环.while循环中的条件是否正确?当我测试它时,它只使用一个条件,但当我添加第二个条件时停止.
我是一个java初学者,我写了这段代码:
class Friends {
public static void main(String[] args) {
String[] facebookFriends = { "John", "Joe", "Jack", "Lucy", "Bob", "Bill", "Sam", "Will" };
int x = 0;
while (x <= 8) {
System.out.println("Frind number " + (x + 1) + " is " + facebookFriends[x]);
x++;
}
System.out.println("");
if (facebookFriends.length < 5) {
System.out.println("Where are all you're friends?");
}
else if (facebookFriends.length == 5) {
System.out.println("You have a few friends...");
}
else {
System.out.println("You are very sociable!");
}
}
} …Run Code Online (Sandbox Code Playgroud)