我一直在制作一个非常简单的程序,询问用户是否喜欢苹果或橙子.如果用户写苹果,您将收到消息"您更喜欢苹果",反之亦然.如果用户未能写"苹果"或"橙子",则会提示他们再次执行此操作.
然而,出于某种原因,无论用户是写"苹果"还是"橙子",它仍然会提示他们再次写回答.这是一个例子.
这是我的代码:
question = input('Do you prefer apples or oranges? ').lower()
while question!='apples' or question!='oranges':
question = input('Do you prefer apples or oranges? ').lower()
print('You prefer ' + question)
Run Code Online (Sandbox Code Playgroud) 任务是while仅使用循环打印以下形状.
*
**
***
****
*****
******
*******
********
*********
Run Code Online (Sandbox Code Playgroud)
以下代码是我已经尝试过的,但不幸的是它不起作用:
#include "stdafx.h"//Visual Studio 2015
#include <stdio.h>
#include <stdlib.h>// using for command system("pause") ;
#include <math.h>
int main()
{
int i=0, k=0;
while (i < 10)
{
while (k <= i)
{
printf("*");
k++;
}
printf("\n");
i++;
}
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不能自己调试它.有人可以为我调试这个吗?
我需要创建一个脚本,它将发送请求(数组中的数据)连续发送到服务器并获得响应,但我还需要在每第5或第6次迭代之间进行延迟(大约3-5秒).例如:
1st data
2nd data
3rd data
4th data
5th data
** 5sec. delay - wait here**
6th data
7th data
...etc...
Run Code Online (Sandbox Code Playgroud)
发送和接收数据没问题,但是我无法在我的代码中正确实现setTimeout以便完全按照我的需要工作.
非常感谢大家,他们将能够修复此代码.
<!DOCTYPE html>
<html>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = [];
var code = [
"WOICEL0Q9P",
"ZJTS4GYJEJ",
"HJPMQOCX31",
"MP26N0BH01",
"7TJNYZIRJR",
"Z5MIDDG4N2",
"BX6MKYK0O7",
"KVFVH1ESQX",
"40ADY3ZBE5",
"V4NT360JR5",
"FDI8AFL680",
"ZH89N59XQR",
"M6OS2OX38H",
"D8O76YDLM0",
"86GBMJLIXY",
"1QRFVU26VK",
"HFUI9QV6DY",
"VN83OGR825",
"DDMPCBX2MF",
"2M3QFPI234"
];
var i = code.length;
var j = code.length; …Run Code Online (Sandbox Code Playgroud) 我有一个生成两个列表的程序.我想从list1打印一个项目,然后切换到从列表2打印项目,然后从list1 ..etc返回打印.然而,每当我尝试它时,它只打印list1然后list2.
请帮忙.
码:
List1 = ['a', 'b' , 'c', 'd', 'e', 'f']
List2 = ['1', '2', '3', '4', '5', '6']
continue = True
while continue == True:
for i in List1:
print i
print '/n'
continue = False
while continue == False:
for i in List2:
print i
print '/n'
continue = True
Run Code Online (Sandbox Code Playgroud)
输出:
a
b
c
d
e
f
1
2
3
4
5
6
Run Code Online (Sandbox Code Playgroud)
期望的输出:
a
1
b
2
c
3
d
4
e
5
f
6
Run Code Online (Sandbox Code Playgroud) 我的代码:
i=0
a = range(100)
while i < range(100):
print i
i += 9
Run Code Online (Sandbox Code Playgroud)
这进入了无限循环...我可以知道为什么吗?
是因为整数与列表比较?但当我变得超过99时会发生什么?
它不应该出现在while循环中吗?
下面的代码正常工作正常:
i=0
a = range(100)
a_len = len(a)
while i < a_len:
print i
i += 9
Run Code Online (Sandbox Code Playgroud) 我有一段带有while循环的代码,我想通过setTimeout()来停止.但它似乎是一个无限循环,从不触发setTimeout().如果我删除while循环,则超时触发正确.有什么问题吗?
$(document).ready(function()
{
var i = 0, s = false;
setTimeout( function()
{
s = true;
console.log( "Timeuot!!!" );
console.log( "s value is " + s );
}, 1000 );
while( s === false )
{
console.log( "this is while and s is " + s );
i++;
}
console.log( "iterations: " + i );
});
Run Code Online (Sandbox Code Playgroud) 我正在split()用C++ 实现一个类似python的函数来训练自己.我从这个SO线程中得到了这个想法:使用字符串分隔符(标准C++)在C++中解析(拆分)一个字符串
在这段代码中:
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
std::cout << token << std::endl;
s.erase(0, pos + delimiter.length());
}
Run Code Online (Sandbox Code Playgroud)
值os pos在while循环条件内分配.
我试过同样的事情:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
std::vector<std::string> split(std::string inp, std::string delimeter){
std::vector<std::string> res;
while (size_t pos = inp.find(delimeter) <= inp.length()){
std::cout << inp << " " << pos << std::endl ;
if (inp.substr(0, delimeter.length()) == delimeter) {
inp.erase(0, delimeter.length());
continue;
}
res.push_back(inp.substr(0, pos));
inp.erase(0, pos); …Run Code Online (Sandbox Code Playgroud) 我正在研究一个实践问题,我们要在一个函数参数中输入一个列表,它将代表一个tic tac toe board,并返回董事会的结果.也就是说,X获胜,O获胜,平局或无(空字符串).
我已经解决了,但我想知道是否有一种方法可以将我的算法操作成循环,因为建议使用循环来比较主对角线的每个元素与其相交的行和列的所有元素,然后检查两个对角线.我是python的新手,所以我的解决方案可能需要更长的时间.如何实施一个循环来检查tic tac toe board的结果?
def gameState (List):
xcounter=0
ocounter=0
if List[0][0]==List[0][1] and List[0][0]==List[0][2]:
return List[0][0]
elif List[0][0]==List[1][0] and List[0][0]==List[2][0]:
return List[0][0]
elif List[0][0]==List[1][1] and List[0][0]==List[2][2]:
return List[0][0]
elif List[1][1]==List[1][2] and List[1][1]==List[1][0] :
return List[1][1]
elif List[1][1]==List[0][1] and List[1][1]==List[2][1]:
return List[1][1]
elif List[1][1]==List[0][0] and List[1][1]==List[2][2]:
return List[1][1]
elif List[2][2]==List[2][0] and List[2][2]==List[2][1]:
return List[2][2]
elif List[2][2]==List[1][2] and List[2][2]==List[0][2]:
return List[2][2]
elif List[2][2]==List[1][1] and List[2][2]==List[0][0]:
return List[2][2]
for listt in List:
for elm in listt:
if elm=="X" or elm=="x": …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <string.h>
int main(void)
{
char str1[1000];
int i, letter, space = 0;
char ch = str1[i];
printf("Enter a sentence: ");
scanf("%[^\n]s", str1);
printf("you enter %s\n", str1);
while (i != strlen(str1)) {
if (ch != ' ') {
letter++;
} else if (ch = ' ') {
space++;
}
i++;
}
printf("%d %d", letter, space);
}
Run Code Online (Sandbox Code Playgroud)
我的while循环不起作用,我似乎无法找到问题.我在ubuntu中使用终端,在打印用户字符串后,我得到一个空行.我必须使用Ctrl-Z来停止脚本.
我有以下for循环,我提示用户输入一个4位数的引脚并按Enter键.有人可以向我解释while循环真正在做什么,因为我不完全理解它.
//user input for pin
for(i = 0; i < PIN_LENGTH; i++)
{
printf("Enter digit %d of your PIN: ", i);
user_pin[i] = getchar();
while (getchar() != '\n'); //what is this line doing??
}
Run Code Online (Sandbox Code Playgroud) while-loop ×10
python ×4
c ×3
for-loop ×3
javascript ×2
c++ ×1
getchar ×1
list ×1
loops ×1
python-2.7 ×1
settimeout ×1