我一直遇到这个python代码的问题.我刚刚开始编码,我无法弄清楚它为什么不起作用.我不能让循环停止重复.无论我输入什么,它都会启动add_item功能.有小费吗?
supply = { #Creates blank dictionary
}
print "Would you like to add anything to the list?"
def add_item(*args): #Adds a item to the supply dictionary
print "What would you like to add?"
first_item = raw_input()
print "How much does it cost?"
first_price = raw_input()
supply[first_item] = float(first_price)
response = raw_input()
while response == "yes" or "Yes" or "YES":
if response == "yes" or "Yes" or "YES": #Added this because it wasn't working, didn't help
add_item()
print "Alright, your …Run Code Online (Sandbox Code Playgroud) 刚开始学习编程..在这里我的问题,使用Python我怎么能在另一个while循环中写一个while循环?
while A > 10:
B +=10
while x < 12:
x +=1
A = bla bla some function depending on B
Run Code Online (Sandbox Code Playgroud)
怎样才是正确的语法?
非常感谢!
所以我不得不写一个简单的代码来计算3N + 1方程; 其中N是用户输入的整数,如果是N = N/2的正整数,并且如果是N = N*3 + 1的负整数.
但是根据我的理解,我的代码在第一个while循环后不起作用,因此不打印任何内容.我究竟做错了什么?编程和学习的新手,所以我感谢你的帮助:)
码:
import java.util.Scanner;
public class ThreeNplusOneProgram {
public static void main(String[] args) {
int N; Scanner input = new Scanner(System.in); int counter;
System.out.println("Please Enter an integer: ");
N = input.nextInt();
while ( N <= 0 ) {
System.out.println("ERROR: Please Enter an integer greater than zero: ");
N = input.nextInt();
}
//So far we know that N is great than Zero
System.out.println(N);
counter = 1;
while ( N != …Run Code Online (Sandbox Code Playgroud) 我试图在2周时间内完成C#的练习,为我的时间限制测试做准备,并且一直试图完成我在书籍和互联网上发现的练习.
该练习要求使用while循环来要求用户输入他们的名字,如果它不是"XXX",那么它可以继续循环.但是我得到的问题是,在编写循环之后,它只是继续,用户无法输入"XXX"来停止程序,所以我想知道是否有人知道这个解决方案?
这是我到目前为止写的代码..
String sName;
//Declaring the variable
Console.Write("Enter a name (or XXX to end): ");
sName = Console.ReadLine();
//Prompting user to enter the name or to end the program by telling them to type XXX
while (sName != "XXX")
{
Console.Write("The Name is: " + sName);
Console.WriteLine();
}
//Start loop
Console.WriteLine("You are now past the while loop");
//If XXX is typed, message is displayed
Console.WriteLine("Press any key to close");
Console.ReadKey();
//Prevent program from closing
Run Code Online (Sandbox Code Playgroud) 我需要使用while循环显示1-100之间的所有偶数.我无法管理它.我认为它应该是这样的:
int e = 1;
while (( 1 < e ) && ( e < 100 )) {
e = e + 1;
if (e % 2==0) {
System.out.print(" " + e);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 我这样做了:
while ( e <= 100 ) {
e = e + 1;
if ( e % 2 == 0)
{
System.out.print(" " + e);
}
}
Run Code Online (Sandbox Code Playgroud) 这是重复的函数(调用get_next_value以获取潜在值!),直到产生有效值(1-26范围内的数字).Get_next_value只是一个函数.但它创造了一个无限循环,我将如何解决它?
while get_next_value(deck) < 27:
if get_next_value(deck) < 27:
result = get_next_value(deck)
return result
Run Code Online (Sandbox Code Playgroud) 我正在努力使程序重复接受输入并重复它直到输入"退出".现在循环没有运行,我不知道为什么因为exit变量设置为false.这是我的代码:
var read = require("read");
var exit = false;
function OutsideLoop (exit) {
while(exit === false){
read({prompt: "> "}, function (err, result) {
console.log("");
console.log(result);
console.log("Type in more input or type 'exit' to close the program.");
if(result === "exit"){
exit = true;};
});
};
};
OutsideLoop();
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.我有一个类似的循环使用if/then而不是while,所以我沿着相同的行重写了这个.
do{
printf("Monster name:%s\n",monsternames[globalvar.monstercatego][globalvar.monsternivel]);
printf("Monster Life:%d\n",globalvar.monsterhp);
printf("------------------------------------------\n");
printf("----------------BattleGround--------------\n");
printf("------------------------------------------\n");
printf("Player name:%s\n", nomeheroi);
printf("Player life:%d\n", globalvar.playerhp);
printf("----------------------------------------\n");
printf("------------------Menu------------------\n");
printf("----------------------------------------\n");
printf("A - Attack\n");
printf("D - Defend\n");
scanf(" %c",&opcaobattle);
switch(opcaobattle)
{
case 'a':
danoPMonster();
break;
case 'd':
break;
}
}while((globalvar.monsterhp >= 0) || (globalvar.playerhp >= 0));
Run Code Online (Sandbox Code Playgroud)
我的控制台显示:
Monster name:Rat
Monster Life:2
----------------------------------------
----------------BattleGround------------
----------------------------------------
Player name:Test
Player life:10
----------------------------------------
------------------Menu------------------
----------------------------------------
A - Attack
D - Defend
a
Monster name:Rat
Monster Life:-1
----------------------------------------
----------------BattleGround------------
----------------------------------------
Player name:Test
Player life:10
----------------------------------------
------------------Menu------------------
----------------------------------------
A - …Run Code Online (Sandbox Code Playgroud) 这是我的问题:
一名学生决定通过稍微粉碎来提前庆祝感恩节的开始.
她的家位于主街0号角,监狱位于主街10号角.学生从第5角开始,向左或向右徘徊一个角,概率为0.5; 她重复这个过程,直到安全抵达家中或登陆监狱.也就是说,在每个角落,醉酒的学生向左或向右错开50-50的概率,到下一个更高编号或下一个更低编号的角落.
使用while或do-while循环编写名为drunkWalk()的方法,模拟醉酒学生的步行; 你的方法应该返回一个整数,这个整数都表示采取了多少步骤以及学生是在监狱里还是在家里.如果你将这个人作为负数返回,如果这个人进入监狱,你可以这样做,如果这个人最终在家,则可以作为正数.您不应该打印出最终版本的方法中的每个步骤,尽管您可能希望在调试时执行此操作.
一旦你的方法工作,让你的主程序调用你的drunkWalk()方法N次(其中N是一个最终的变量).最后,计算并打印学生一次旅行所需的平均步数.这是你的程序在行动中的样子,N等于5:
Here we go again... time for a walk! <br/>
Took 37 steps, and <br/>
Landed at HOME<br/>
Here we go again... time for a walk!<br/>
Took 19 steps, and <br/>
Landed in JAIL<br/>
Here we go again... time for a walk!<br/>
Took 13 steps, and <br/>
Landed in JAIL<br/>
Here we go again... time for a walk! <br/>
Took 25 steps, and <br/>
Landed in JAIL<br/>
Here we go again... time for a …Run Code Online (Sandbox Code Playgroud) 我希望程序在捕获异常时重新执行while循环 - 接收文本输入的异常.相反,它继续使用下面的代码循环,我希望它再次要求用户输入.
import java.util.InputMismatchException;
import java.util.Scanner;
public class NumberGuess {
public static void main(String[] args) {
Scanner Scanner = new Scanner(System.in);
int between = 100;
int secretNumber = (int)(Math.random()*between);
int inputNum = 0;
int guesses = 0;
System.out.println("Please enter your guess: ");
inputNum = Scanner.nextInt();
guesses++;
// #### Loop here ####
while (inputNum != secretNumber) {
// Try catch
try {
// number too high or too low
if (inputNum > 100 | inputNum < 0) {
System.out.println("Please enter …Run Code Online (Sandbox Code Playgroud) while-loop ×10
java ×4
loops ×4
python ×3
c ×1
c# ×1
dictionary ×1
javascript ×1
node.js ×1
numbers ×1
parameters ×1
random ×1
return ×1
system.out ×1
try-catch ×1