标签: while-loop

如何使用gpiozero button.when_pressed函数使用输入和输出整数的函数?

我正在尝试对Raspberry Pi上的按钮进行编程,以将一个整数添加到另一个整数,以便可以while通过检查变量mod 2是否为0来在循环中的条件之间来回切换。我本质上是在尝试while通过检查变量是奇数还是偶数来翻转循环中的条件。

我正在尝试使用该gpiozero库的when_pressed函数,但是它似乎无法调用添加和输出整数的函数。

所以,我的代码是:

from gpiozero import Button
btn = Button(17) #the button is wired to GPIO pin 17

def addSurf(a):
    a = a + 1
    return(a)

x = 0
btn.when_pressed = addSurf(x)

while True:
    if x == 0:
        #do some stuff
    else:
        #do some other stuff
Run Code Online (Sandbox Code Playgroud)

为什么我尝试运行此程序,我明白了TypeError: unsupported operand type(s) for +: 'Button' and 'int'。

如何使用该btn.when_pressed函数使用输入和输出整数的函数?

或者,是否还有其他[更好的]方法来使按钮在while循环中切换两个状态?

python function while-loop gpio raspberry-pi

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

在bash中更改字符串类型变量的分隔符

我的任务很小.

我应该写:

data="duke,rock,hulk,donovan,john"
Run Code Online (Sandbox Code Playgroud)

在下一个变量中,我应该更改第一个变量的分隔符.

data2="duke|rock|hulk|donovan|john"
Run Code Online (Sandbox Code Playgroud)

在bash上执行此操作的正确方法是什么?

这是脚本的一小部分,我应该做什么.

例如,我使用构造"WHILE-GETOPS-CASE"来在参数中使用用户名来排除它们.

ls /home/ | egrep -v $data2
Run Code Online (Sandbox Code Playgroud)

bash shell sh while-loop

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

通过重新为变量赋值来循环Elixir

我想在Elixir中编码这个算法:

var name = generate_name();
while (check_if_exists(name)) {
  name = generate_name();
}
Run Code Online (Sandbox Code Playgroud)

我无法在Elixir中以相同的方式编码,因为必须有更多的惯用和功能方式.那怎么办呢?

loops elixir while-loop

1
推荐指数
2
解决办法
4941
查看次数

使用while迭代带有条件的列表?

我试图遍历一个素数列表,但是当我达到一定限度时我想停止.

def primetest(number,primelst):
    limit = int(round(sqrt(number),0)+1)
    while prime in primelst < limit:
        if (number % prime) == 0:
            return False
    return True
Run Code Online (Sandbox Code Playgroud)

作为FOR语句,这有效:

def primetest(number,primelst):
    limit = int(round(sqrt(number),0)+1)
    for prime in primelst:
        if (number % prime) == 0:
            return False
    return True
Run Code Online (Sandbox Code Playgroud)

但这很慢,因为它遍历所有素数,我希望迭代在达到极限时停止.所以while声明似乎是一个更好的选择,但我无法弄清楚正确的语法.

python list while-loop

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

C++ while循环从输入文件中读取

我编写了一个函数,使用while循环从输入文件中读取事务.我不能为我的生活弄清楚为什么它会读两遍最后两行.使用时

 while(InFile){code}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,它将继续循环,直到文件到达EOF标记.我无法弄清楚我在哪里错了.

void ProcessTransactions(Bank &acctList, string fileName)
{

    Date transDate;
    ifstream InFile;
    InFile.open(fileName.c_str());
    int month;
    int day;
    int year;
    int acctNum;
    int transAcctNum;
    float amount;
    string transType;

    while(InFile)
    {
        InFile >> month >> day >> year;
        transDate.SetDate(month, day, year);

        InFile >> acctNum;
        InFile >> amount;
        InFile >> transType;
        if(transType == "Transfer")
            InFile >> transAcctNum;

        cout << amount << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

输入文件

5 1 2012    1212    100.00  Deposit
5 1 2012    2323    100.00  Deposit
5 1 2012    3434    100.00 …
Run Code Online (Sandbox Code Playgroud)

c++ loops while-loop

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

while循环输出多个条件

main()
{
    int i=0,j=0;
    while(i<5,j<10)
    {
        i++;
        j++;
    }
    printf("%d,%d,",i,j);
}
Run Code Online (Sandbox Code Playgroud)

输出: 10,10

int main(){
    int x=2,y=2;
    while(x<=5,y<=3)
         printf("%d %d ",++x, ++y);
    return 0;        
}
Run Code Online (Sandbox Code Playgroud)

输出: 3 3 4 4

在第一个代码中,输出如何产生10,10?任何人都可以解释,但第二个代码的输出是3344,是否都运行在不同的逻辑上?

c loops while-loop conditional-statements

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

为什么这个c程序陷入无限循环?

#include <stdio.h>

int main()
{
  int sum=0;
  char s[10];

  while(scanf("%[^\n]s", s)!=EOF)
  {
    printf("%s", s);

  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个while循环落入任何字符串输入的无限循环.

c loops scanf while-loop

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

当我进入退出条件时,为什么程序打印两次默认条件而不是退出while循环?

我是初学者,我从一本书中复制了这段代码,但是它不起作用,因为当我输入EOF时,程序而不是退出while循环会打印两次默认消息.这就像一个小时,我寻找一个解决方案,但我尝试过的任何工作,谢谢你的帮助.该计划应该计算每种等级的数量.

#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <math.h>
#include <ctype.h>

using namespace std;


int main()
{ 

    int grade,
        aCount = 0,
        bCount = 0,
        cCount = 0,
        dCount = 0,
        fCount = 0;



    cout << "Enter the letter grades (EOF to quit) ->>";

    while ((grade = cin.get()) != 'EOF')  {

        switch (grade) {

        case 'A':
            ++aCount;
            break;

        case 'B':
            ++bCount;
            break;

        case 'C':
            ++cCount;
            break;

        case 'D':
            ++dCount;
            break;

        case 'F':
            ++fCount;
            break;

        case '\n':
        case '\t':
        case ' …
Run Code Online (Sandbox Code Playgroud)

c++ while-loop switch-statement visual-c++

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

为什么我的JSON数组没有以正确的格式返回?

当我回应$json2我JSON Array的回归时:

[{"contact_phonenumber":"12345"}][{"contact_phonenumber":"67890"}][{"contact_phonenumber":"123456"}][{"contact_phonenumber":"78901"}][{"contact_phonenumber":"234567"}][{"contact_phonenumber":"8901234"}]
Run Code Online (Sandbox Code Playgroud)

它看起来不像JSON Array我,我确定我的循环有问题 - 也许是因为while它嵌套在for each或者什么东西,但即使我改变它,我仍然得到相同的结果.你能帮我吗?

我想要它JSON array像:

[{"contact_phonenumber":"12345"}, {"contact_phonenumber":"67890"},
 {"contact_phonenumber":"123456"}, {"contact_phonenumber":"78901"},{"contact_phonenumber":"234567"}, {"contact_phonenumber":"8901234"}]
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

<?php

require('dbConnect.php');

//this is the username in the user table
$Number = "+353872934480";

// get the username of the user in the user table, then get the matching user_id in the user table
// so we can check contacts against it 
$query = "SELECT * FROM user WHERE username = ?";
$stmt …
Run Code Online (Sandbox Code Playgroud)

php while-loop

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

虽然循环使Unity崩溃,但不是无限的

我从类似的帖子中知道,无限的while循环因导致Unity3d崩溃而臭名昭著。我想隐含地在我正在处理的东西中进行一会儿循环,我相当确定这不是“无限”,但会导致游戏崩溃。

逻辑的想法是检查整数列表中的连续数字,并以此为基础应用加分。该列表包含“有效射击”,并且每次射击时都会添加一个新的int-有效射击越连续,奖金就越高。

这是我所拥有的:

        int count = 0;
        int firstItem = 0;
        int multiples = 3;
        int currentMultiple = 0;
        int bonusX = 0;

        foreach (int i in effectiveShots)
        {
            if (count == 0)
            {
                firstItem = i;
                count = 1;
            }

            else if (i == firstItem + count)
            {
                count++;
            }

        }

        while (count >= multiples)
        {
            currentMultiple = multiples;

            if (count == currentMultiple + multiples)
            {
                bonusX += 1;
            }

            if (bonusX > 10 || gameOver)
                break;

            UnityEngine.Debug.Log(bonusX);
        } …
Run Code Online (Sandbox Code Playgroud)

c# infinite-loop while-loop unity-game-engine

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