标签: while-loop

为什么我的python程序没有关闭

如果用户没有输入yes,y,no或n,我希望此代码停止循环该函数

go = True
def levelOne():
    print "You are in a room"
    print "There is a table, on the table there is a key"
    print "There is a door to the north"
    print "Use the key to open the door and escape?"
    userInput = raw_input()
    str(raw_input).lower()
    if userInput == "y" or userInput == "yes":
        print "Ok"
    elif userInput == "n" or userInput == "no":
        print "Fine, die then"
    else:
        go = False
While go == True:
    levelOne()
Run Code Online (Sandbox Code Playgroud)

现在它无限循环,为什么会这样?

python while-loop

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

while循环在python中有多个条件

weatherType = raw_input('Enter a weather type: ')

while (weatherType != "WINDDIRECTION") or (weatherType != "WINDSPEED") or (weatherType != "AIRTEMPERATURE") or (weatherType != "WAVEHEIGHT") or (weatherType != "AIRPRESSURE"):
    print "Sorry, invalid input. Please enter AIRTEMPERATURE, AIRPRESSURE, WAVEHEIGHT, WINDSPEED, or WINDDIRECTION  for a city and either WINDDIRECTION, WINDSPEED, or AIRTEMPERATURE for an off shore bouy"
    weatherType = raw_input('Enter a weather type: ')
Run Code Online (Sandbox Code Playgroud)

好了,所以用这个循环中,我试图让用户输入要么WINDDIRECTION,WINDSPEED,AIRTEMPERATURE,WAVEHEIGHT,或AIRPRESSURE.但是,即使用户输入这5个选项中的1个,我的代码仍然会进入while循环.我不知道发生了什么事.我知道我可以使用for循环(对于["WINDDIRECTION",....]中的x)但是for循环只能查看他们的输入是否是第一次是正确的,如果他们再输入错误的答案代码将继续

python while-loop multiple-conditions

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

Pythonic的方式来计算计算器的函数 - Python

我正在创建一个文本计算器,它不能完全按常规工作.它的工作原理如下:

  • 操作输入:用户输入计算器将处理的操作(+用于加法,-用于减法,*用于乘法或/用于除法)
  • 操作数输入:用户在计算的每个操作数中输入,按操作数之间的Return键.一旦用户输入了所有他想要的操作数,击中Return三次就会启动计算.

计算:

当用户输入两个以上的操作数时,而不是:

operand1 plus/minus/times/divided by operand2,

它确实:

operand1 plus/minus/times/divided by operand2 plus/minus/times/divided by operand3,

等等所有输入的操作数.

这个代码只是循环输入操作数列表operandList,并使用所选择的操作operation(并且result是计算的结果):

def Calculate():
    global operation, operandList, result
    operateCount = 0
    result = 0
    while operateCount < len(operandList):
        if operation == '+':
            result += operandList[operateCount]
        elif operation == '-':
            result -= operandList[operateCount]
        elif operation == '*':
            result *= operandList[operateCount]
        elif operation == '/': …
Run Code Online (Sandbox Code Playgroud)

python if-statement while-loop

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

虽然浏览器和OS上的编译程序之间存在真正的区别

今天我在他的Raspberry Pi上看了我的程序的同事用Python编写的小游戏.游戏引擎使用一个while(true)或无限循环运行.我自己在PHP中使用websocket程序while(true).在这些示例中,使用无限循环,这不会导致程序无响应.没有它,它将无法正常工作.但是在JavaScript中我们需要不惜一切代价避免无限循环.当循环变为无限时,浏览器会挂起并变得无响应.

我的问题:是什么之间的区别无限循环中运行编译的Python程序和一个无限循环写的JavaScript在浏览器中运行?

javascript infinite-loop while-loop

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

当a是数字时,while(!a)是什么意思?

试图破译这段C代码:

int WaitForPacket(uint16 milliseconds, Dexcom_packet* pkt, uint8 channel) {
uint32 start = getMs();
uint8 * packet = 0;
uint32 i = 0;
uint32 seven_minutes = 420000;
int nRet = 0;
swap_channel(nChannels[channel], fOffset[channel]);

while (!milliseconds || (getMs() - start) < milliseconds) {
    i++;
    if(!(i % 60000)) {
        strobe_radio(channel);
    }
    doServices();
    if((getMs() - start) > seven_minutes) {
        killWithWatchdog();
        delayMs(2000);
    }
    blink_yellow_led();
    if (packet = radioQueueRxCurrentPacket()) {
        uint8 len = packet[0];
        fOffset[channel] += FREQEST;
        memcpy(pkt, packet, min8(len+2, sizeof(Dexcom_packet)));
        if(radioCrcPassed()) {
            if(pkt->src_addr == dex_tx_id …
Run Code Online (Sandbox Code Playgroud)

c conditional while-loop

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

我如何摆脱这种无限循环?

##
numOfYears = 0
## Ask user for the CPI
cpi = input("Enter the CPI for July 2015: ")
## If they didn't enter a digit, try again
while not cpi.isdigit():
    print("Bad input")
    cpi = input("Enter the CPI for July 2015: ")
## Convert their number to a float
cpi = float(cpi)
while cpi <= (cpi * 2):
    cpi *= 1.025
    numOfYears += 1
## Display how long it will take the CPI to double
print("Consumer prices will double in …
Run Code Online (Sandbox Code Playgroud)

python infinite-loop while-loop

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

使用if或while递归时的C差异

在尝试找到编写递归因子函数的不同可能性时,我注意到我对使用while或if在下面的递归函数中的差异感到困惑.我看了每个选项的步骤使用gdb debugger,也看不出任何区别.我知道它的功能while loop,并且它可以迭代地工作.但由于它的主体包含函数的递归调用作为return语句,当前函数应该是'left'并且应该输入一个新函数,这将导致进入一个新的while循环?在我看来,在这种情况下,它的作用就像一个普通的if statement?我对此有任何澄清表示感谢.

#include <stdio.h>
#include <stdlib.h>

int recursion(int n)
{
    while(n > 0)
        return n * recursion(n-1);
    return 1;
}

int main(void) 
{
    int n = 5;

    printf("%d\n", recursion(n));       

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

使用if导致相同结果的替代递归函数:

int recursion(int n)
{
    if(n > 0)
        return n * recursion(n-1);
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

c recursion if-statement while-loop

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

打破我的循环

我已经为程序创建了基本结构,但无论我尝试什么,我似乎都无法退出循环.

我还希望它显示用户输入的信息; 一旦我退出循环,从键盘到控制台,但我不知道如何做到这一点.

import java.util.Scanner;

public class Requirement1 {

    public static void main(String[] args) {

        Scanner scan = new Scanner (System.in);
        String name, game, time, points;
        int i = 0;

        System.out.println("Please Enter Your Name");
        name = scan.nextLine();
        System.out.println("Players Name: " + name);
        while (i < 100)
        {

        Scanner scan2 = new Scanner(System.in);
        System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
        game = scan.nextLine();
        System.out.println("Game: " + game);

        Scanner scan3 = new Scanner (System.in);
        System.out.println("Please Enter Your Score"); …
Run Code Online (Sandbox Code Playgroud)

java eclipse arrays break while-loop

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

当product = 9时,为什么程序不停止?

该程序应打印出3,6和9然后停止并显示"程序结束",但继续打印3,6,9和12.如何解决?示例代码如下:

int x = 1;
int mul = 3;
int product = 0;
while (product < 10) {
    product = mul * x;
    System.out.println(product);
    x++;
}
System.out.println("End Of Program Reached");
Run Code Online (Sandbox Code Playgroud)

java loops while-loop

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

从循环打印

我需要打印一个完美数字的因子.这是我主要课程的要点:

ArrayList<Integer> perfNums = new ArrayList<>();
Scanner in = new Scanner(System.in);
System.out.print("Enter the upperbound: ");
upperbound = in.nextInt();
for (int i = 1; i <= upperbound; i++) {
     if (isPerfect(i)) { //boolean to check if number is a perfect number
        perfNums.add(i);
     }
}

System.out.println("Perfect numbers between 1 and " + upperbound + " are:");
for (int i = 0; i < perfNums.size(); i++) {
     System.out.print(perfNums.get(i) + " = ");
     printFactor((int)perfNums.get(i));
     System.out.println();
}
Run Code Online (Sandbox Code Playgroud)

这是printFactor类.

private static void printFactor(int number){ …
Run Code Online (Sandbox Code Playgroud)

java loops numbers while-loop factors

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