标签: while-loop

如何将raw_input()与while循环一起使用

只是尝试编写一个程序,将用户输入并将其添加到列表'数字':

print "Going to test my knowledge here"
print "Enter a number between 1 and 20:"

i = raw_input('>> ')
numbers = []

while 1 <= i <= 20 :
    print "Ok adding %d to numbers set: " % i 
    numbers.append(i)

    print "Okay the numbers set is now: " , numbers
Run Code Online (Sandbox Code Playgroud)

但是,当我执行程序时,它只运行到raw_input()

Going to test my knowledge here
Enter a number between 1 and 20:
>>> 4
Run Code Online (Sandbox Code Playgroud)

我在这里缺少一些基本规则吗?

python raw-input while-loop

4
推荐指数
1
解决办法
9956
查看次数

while(i - > 0)是什么意思?

如果这是一个愚蠢的问题,我道歉,但我无法在任何地方找到答案.

以下代码如何工作?(我意识到它循环于元素els)

var i = els.length;
while (i --> 0) {
    var el = els[i];
    // ...do stuff...
}
Run Code Online (Sandbox Code Playgroud)

我不知道是什么-->意思.它没有文档.有人可以开导我吗?

javascript operators while-loop

4
推荐指数
2
解决办法
5322
查看次数

当换行符位于格式字符串中时 scanf 的行为

下面是我的代码的副本。基本上,我需要创建一个程序,根据“工资代码”(例如工人的职位)计算工资。我已经创建了我的 switch 语句,除了我输入第一个付款码时的一开始,一切正常。我输入第一个付款代码,它会转到下一行,将其留空。我输入另一个数字,它会按照预期的方式运行该数字和前一个数字。然后,一切正常。我确信这是一个简单的解决方案,但对我来说有点棘手。另外,我不确定应该如何在这里格式化我的代码,使其看起来像在服务器上一样,所以我对它看起来令人困惑的方式表示歉意。

#include <stdio.h> //precompiled header
int main(void)
{
    //declare variables
    unsigned int counter = 0; //counters to calculate amount of workers paid
    unsigned int counter2 = 0;
    unsigned int counter3 = 0;
    unsigned int counter4 = 0;

    int paycode;
    float overtime; //amount of overtime hours
    float salary; //weekly salary
    float hoursWorked;
    float hourlyRate;
    float grossWeeklySales; //weekly sales for commissioned workers
    int itemsProduced;
    float fixedAmount; //money given per item produced

    //prompt for input
    printf("Please enter the employee's paycode.\n");
    printf("1: …
Run Code Online (Sandbox Code Playgroud)

c counter while-loop switch-statement

4
推荐指数
1
解决办法
1193
查看次数

如何停止PHP脚本?

我写过像这样的PHP代码:

<?php
    $i=0;
    while($i<100) {
       mail("xxxxxxx@gmail.com","SPAM","It´s ".date("d.m.o H:i:s");
       $i++;
    }
    echo "DONE!";
?>
Run Code Online (Sandbox Code Playgroud)

我已将其保存在webserver目录中.我通过浏览器启动它.如何在执行期间停止脚本?

最好的祝福 ;)

php apache email loops while-loop

4
推荐指数
1
解决办法
1万
查看次数

Java - 程序没有突破循环?

while (choice != 7) { 

    System.out.println("---  Mathematical Calculator  ---");

    System.out.println("");

    System.out.println("Pick an operation from the list - Use nos. 1 to 7");
    System.out.println("1) Multiplication");
    System.out.println("2) Division");
    System.out.println("3) Addition");
    System.out.println("4) Subtraction");
    System.out.println("5) Find the area of a regular object");
    System.out.println("6) Find the volume of a regular object");
    System.out.println("7) Exit");

    choice = userInput.nextInt(); 

    switch (choice) {

    case 1: {
        System.out.println("");
        System.out.println("You have chosen multiplication");
        System.out.println("Enter a number");
        double num1 = userInput.nextDouble();
        System.out.println("Enter another number");
        double num2 = userInput.nextDouble();

        double num3 = num1 …
Run Code Online (Sandbox Code Playgroud)

java while-loop

4
推荐指数
1
解决办法
56
查看次数

Javascript中的+ =是什么?

例如,在while循环中:

while (i < 10) {
    text += "The number is " + i;
    i++;
}
Run Code Online (Sandbox Code Playgroud)

它不能大于或等于,因为就是这样:> =那它是做什么的?谢谢

javascript loops while-loop

4
推荐指数
2
解决办法
2万
查看次数

有没有理由在Python中使用"while 1,do something,break"?

在Python库SymPy我试着去理解函数partitions()中sympy.utilities.iterables:

它是这样开始的:

def partitions(n, m=None, k=None, size=False):
    """Generate all partitions of integer n (>= 0).
Run Code Online (Sandbox Code Playgroud)

我对以下while循环感到困惑,因为它看起来毫无意义.如果我删除while 1:和break它应该没有什么区别.但是,我希望开发SymPy的人知道他们在做什么,并且不会犯很简单的错误.那么这是否意味着我没有看到?

while 1:
    # Let i be the smallest key larger than 1.  Reuse one instance of i.
    i = keys[-1]
    newcount = ms[i] = ms[i] - 1
    reuse += i
    if newcount == 0:
        del keys[-1], ms[i]
    room += 1

    # Break the remainder into pieces of size i-1.
    i -= 1
    q, …
Run Code Online (Sandbox Code Playgroud)

python sympy while-loop

4
推荐指数
1
解决办法
768
查看次数

C++ while循环无法启动

因此,我们的想法是向用户询问数组的每个元素,但在为第一个问题(它要求元素数量)提供输入后,没有任何反应.无法弄清楚为什么.

#include <iostream>

int main()
{
        int numGrades;
        tryAgain:
        std::cout << "Enter number of grades" << std::endl;
        std::cin >> numGrades;

            if (numGrades > 30)
                {
                std::cout << "Please enter a valid number of grades" << std::endl;
                goto tryAgain;
                }


        int grades[numGrades - 1];
        int gradeCount = 0;
        while (gradeCount < numGrades);
            {
            std::cout << "Enter grade number" << gradeCount + 1 << ":";
            std::cin >> grades[gradeCount];

            ++ gradeCount;
            }   

        std::cout << grades;
        return 0;   
}
Run Code Online (Sandbox Code Playgroud)

c++ while-loop

4
推荐指数
1
解决办法
188
查看次数

c ++ While循环终止功能

因为它在早些时候工作得很好,但是当我去添加一些其他功能时,我的程序吓坏了,我无法将它恢复到原来的状态.

我让我写一个摇滚/纸/剪刀程序来对抗计算机,任何有关循环不断终止的帮助都会很精彩

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


void RPSout(char);
int RPScomp();

int main() {

    char choice;
    int endit=0;

    while (endit == 0)
    {
        cout << "\n\n\tReady to play Rock/Paper/Scissors against the computer??(please choose R/P/S)(Q to quit)\n";
        cin >> choice;

        RPSout(choice);

        if (choice=='Q'||'q')
            {endit=1;}
    }
    return 0;
}


void RPSout(char choose)
{
    int RPS =0;
    int comp=0;
    switch (choose)
    {
    case 'R':
    case 'r':
    {
        cout <<"Your choice: Rock";
        break;
    }
    case 'P':
    case 'p':
    {
        cout …
Run Code Online (Sandbox Code Playgroud)

c++ loops while-loop srand switch-statement

4
推荐指数
1
解决办法
79
查看次数

在另一个文件中查找大文件中的行的最快方法

我在 while 循环中使用 grep 在另一个文件中查找一个文件中的行并将输出保存到新文件中。我的文件非常大(2.26 亿行),并且脚本需要很长时间(12 天并且还在增加)。您有加快速度的建议吗?也许有比 grep 更好的方法?

(我还需要前一行的输出,因此 grep -B 1。)

这是我的代码:

#!/bin/bash

while IFS= read -r line; do
  grep -B 1 $line K33.21mercounts.bf.trimmedreads.dumps.fa >> 21mercounts.bf.trimmedreads.diff.kmers.K33;
done <21mercounts.bf.trimmedreads.diff.kmers 

Run Code Online (Sandbox Code Playgroud)

更新:

包含要查找的行的输入文件大小为 4.7 GB,有 226 mio 行,如下所示:

AAAGAAAAAAAAAGCTAAAAT
ATCTCGACGCTCATCTCAGCA
GTTCGTCGGAGAGGAGAGAAC
GAGGACTATAAAATTGTCGCA
GGCTTCAATAATTTGTATAAC
GACATAGAATCACGAGTGACC
TGGTGAGTGACATCCTTGACA
ATGAAAACTGCCAGCAAACTC
AAAAAACTTACCTTAAAAAGT
TTAGTACACAATATCTCCCAA
Run Code Online (Sandbox Code Playgroud)

要查看的文件大小为 26 GB,有 20 亿行,如下所示:

>264638
AAAAAAAAAAAAAAAAAAAAA
>1
AAAGAAAAAAAAAGCTAAAAT
>1
ATCTCGACGCTCATCTCAGCA
>1
GTTCGTCGGAGAGGAGAGAAC
>28
TCTTTTCAGGAGTAATAACAA
>13
AATCATTTTCCGCTGGAGAGA
>38
ATTCAATAAATAATAAATTAA
>2
GAGGACTATAAAATTGTCGCA
>1
GGCTTCAATAATTTGTATAAC

Run Code Online (Sandbox Code Playgroud)

预期的输出将是这样的:

>1
AAAGAAAAAAAAAGCTAAAAT
>1
ATCTCGACGCTCATCTCAGCA
>1
GTTCGTCGGAGAGGAGAGAAC
>2
GAGGACTATAAAATTGTCGCA
>1 …
Run Code Online (Sandbox Code Playgroud)

linux bash grep while-loop

4
推荐指数
1
解决办法
520
查看次数

标签 统计

while-loop ×10

loops ×3

c++ ×2

javascript ×2

python ×2

switch-statement ×2

apache ×1

bash ×1

c ×1

counter ×1

email ×1

grep ×1

java ×1

linux ×1

operators ×1

php ×1

raw-input ×1

srand ×1

sympy ×1