作为代码学院课程之后的第一个项目,我正在制作摇滚/纸/剪刀游戏,但我无法看到我的循环在哪里出错.
while (userChoice !== "ROCK" | "PAPER" | "SCISSORS" | "LIZARD" | "SPOCK") {
var userChoice = prompt("Invalid choice. Please pick ROCK, PAPER, SCISSORS, LIZARD or SPOCK.");
var userChoice = userChoice.toUpperCase();
}
Run Code Online (Sandbox Code Playgroud)
道歉,如果它是平凡的.
我想打印两列等于变量的行,例如输入:
2607s1 NC_000067.6 95.92 49 1 1 3 50 1e-14 84.2
2607s1 NC_000067.6 97.73 44 1 0 7 50 4e-14 84.2
2607s1 NC_000067.6 97.67 43 1 0 8 50 1e-13 75.0
Run Code Online (Sandbox Code Playgroud)
和第一列和最后一列的变量:
a="2607s1"; b="84.2"
Run Code Online (Sandbox Code Playgroud)
并使用awk命令,输出:
2607s1 NC_000067.6 95.92 49 1 1 3 50 1e-14 84.2
2607s1 NC_000067.6 97.73 44 1 0 7 50 4e-14 84.2
Run Code Online (Sandbox Code Playgroud)
我尝试了以下但不工作:
awk -v '$1==$a' && '$10==$b' test_file
cat test_file|awk '$1=="($a)" && $10=="($b)"'
cat test_file|awk '$1==($a) && $10==($b)'
cat test_file|awk '$1=="$a" && $10=="$b"'
Run Code Online (Sandbox Code Playgroud)
而且,我在一个while循环中运行它,所以$a并且 …
我写了一个函数,应该多次尝试一个函数,直到这个工作.
def Retry(attempts,back_off,value):
for i in range(attempts):
counter = 0
while attempts > counter:
try:
x = function(value)
except:
counter =+ 1
delay = (counter * back_off) + 1
print ('trying again in {} seconds'.format(delay))
sleep(delay)
continue
break
return x
result = Retry(20,2,value)
Run Code Online (Sandbox Code Playgroud)
每次失败的尝试之后都应该是指数增长的时间间隔,即2秒后的第二次尝试,4秒后的第三次尝试,8秒后的第四次尝试,依此类推.问题在于,在我写的函数中,如果第一次尝试失败,我只会得到一系列无限的行:
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
....
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?为什么循环堆栈在那里?
using System;
using System.Collections.Generic;
using System.Numerics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int charCount = 0;
BigInteger a = 0;
BigInteger b = 1;
List<BigInteger> list = new List<BigInteger>();
while(0 != 1)
{
list.Add(a);
BigInteger c = a; //
a = b; // this is the Fibonacci sequence
b = a + c; //
charCount = a.ToString().Length;
if (charCount >= 1000)
{
Console.WriteLine(list.IndexOf(a));
break;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是针对项目Euler上的问题25编写的,您需要在Fibonacci序列中找到包含1000位数的第一项的索引.
我检查了一下,应用程序工作直到写入list.IndexOf(a).出于某种原因,它将索引写为-1.
通过在循环中使用带有i ++的整数i(如下所示),我能够解决问题,但我很好奇为什么返回-1而不是索引.
int i …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用javascript运行一个超级简单的程序,但我无法理解这个问题.好的,我的代码:
var answer = prompt("Are we there yet?");
while(answer != "yes" || answer != "yeah") {
var answer = prompt("Are we there yet?");
}
alert("Yes! We made it!");
Run Code Online (Sandbox Code Playgroud)
问题是当我在我的浏览器上运行它并键入其中一个"yes"或者"yeah"提示方法一直出现,除非我将逻辑运算符从OR(||)更改为AND(&&).
它不应该与OR(||)一起使用吗?我只是说这两个答案中的任何一个都没问题,警报可以运行吗?
我知道这是非常基本的东西,这可能是一个愚蠢的错误,但我只是一个初学者试图解决这个问题.
提前致谢.
在开始用C语言学习代码之后,我总是假设for循环和while循环,其中essentialy总是等价的(因为在一个中总是可以重现只使用另一个的行为).但是在python中从for循环到while循环总是微不足道的,我找不到实现反转的方法.
在python中,有没有办法只使用for循环来重现while循环(无限循环)的行为?
这是一个使用递归生成器不起作用的解决方案(因为递归限制):
def infinite_loopy():
yield "All work and no play makes Jack a dull boy"
for x in infinite_loopy():
yield x
#here starts the supposedly infinite-loop
for x in infinite_loopy():
print(x)
Run Code Online (Sandbox Code Playgroud) 使用此功能,我生成所需的范围:
first_index = 0
last_index = 3
ranges = []
while first_index != last_index
while last_index != 0
if first_index < last_index
ranges << (first_index..last_index)
end
last_index -= 1
end
first_index += 1
last_index = 3
end
p ranges
Run Code Online (Sandbox Code Playgroud)
输出是:
[0..3, 0..2, 0..1, 1..3, 1..2, 2..3]
Run Code Online (Sandbox Code Playgroud)
我需要while在完成后恢复嵌套循环的输出.所以在这个例子中,我需要:
[0..3, 0..2, 0..1].reverse
[1..3, 1..2].reverse
[2..3].reverse (wouldn't make any different on this, though)
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
[0..1, 0..2, 0..3, 1..2, 1..3, 2..3]
Run Code Online (Sandbox Code Playgroud)
我能reverse以某种方式调用该函数吗?last_index可以是任何整数.我用3来缩短输出.
考虑以下计划.我想知道为什么这些代码以不同的方式表现.提前提前.
这不打印任何
#include <stdio.h>
int main() {
int i = 0;
while(i < 10) {
if(i < 7)
printf("value is%d", i++);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这样做
#include <stdio.h>
int main() {
int i = 0;
while(i < 10) {
if(i < 7)
printf("value is%d\n", i++);
}
}
Run Code Online (Sandbox Code Playgroud) 刚开始学习python并尝试使用循环编写代码来检查唯一字母,如下所示:
def has_unique_letters(x):
count = 0
letters = set(x)
while count < len(x):
if x[count] in letters:
return False
count += 1
return True
Run Code Online (Sandbox Code Playgroud)
但无论我在x中输入什么,它都返回false.代码有什么问题?谢谢!
我在bash中循环使用单行无限,并尝试在条件循环时添加单行.下面我提到了我的命令,它给出了意想不到的结果(假设在2次迭代后停止,但它永远不会停止.并且它还将变量I视为可执行文件).
命令:
i=0; while [ $i -lt 2 ]; do echo "hi"; sleep 1; i = $i + 1; done
Run Code Online (Sandbox Code Playgroud)
输出:
hi
The program 'i' is currently not installed. To run 'i' please ....
hi
The program 'i' is currently not installed. To run 'i' please ....
hi
The program 'i' is currently not installed. To run 'i' please ....
...
...
Run Code Online (Sandbox Code Playgroud)
注意:我在Ubuntu 14.04上运行它
while-loop ×10
loops ×4
python ×3
bash ×2
for-loop ×2
javascript ×2
arrays ×1
awk ×1
c ×1
c# ×1
conditional ×1
if-statement ×1
indexof ×1
linux ×1
reverse ×1
ruby ×1
set ×1
shell ×1
try-except ×1
unix ×1
variables ×1