如果用户没有输入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)
现在它无限循环,为什么会这样?
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循环只能查看他们的输入是否是第一次是正确的,如果他们再输入错误的答案代码将继续
我正在创建一个文本计算器,它不能完全按常规工作.它的工作原理如下:
+用于加法,-用于减法,*用于乘法或/用于除法)计算:
当用户输入两个以上的操作数时,而不是:
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) 今天我在他的Raspberry Pi上看了我的程序的同事用Python编写的小游戏.游戏引擎使用一个while(true)或无限循环运行.我自己在PHP中使用websocket程序while(true).在这些示例中,使用无限循环,这不会导致程序无响应.没有它,它将无法正常工作.但是在JavaScript中我们需要不惜一切代价避免无限循环.当循环变为无限时,浏览器会挂起并变得无响应.
我的问题:是什么之间的区别无限循环中运行编译的Python程序和一个无限循环写的JavaScript在浏览器中运行?
试图破译这段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) ##
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) 在尝试找到编写递归因子函数的不同可能性时,我注意到我对使用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) 我已经为程序创建了基本结构,但无论我尝试什么,我似乎都无法退出循环.
我还希望它显示用户输入的信息; 一旦我退出循环,从键盘到控制台,但我不知道如何做到这一点.
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) 该程序应打印出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) 我需要打印一个完美数字的因子.这是我主要课程的要点:
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) while-loop ×10
python ×4
java ×3
c ×2
if-statement ×2
loops ×2
arrays ×1
break ×1
conditional ×1
eclipse ×1
factors ×1
javascript ×1
numbers ×1
recursion ×1