标签: while-loop

如何在 python 3 中让 while 语句再次运行一次

有没有办法让 while 语句在完成后再次运行一次?

这显然会打印 'hello world' 31 次。尽管在较长的程序中,在循环之后再次重写整个代码有点愚蠢。

任何帮助将不胜感激!

更新!我没有做 x<31 的原因如下。

我正在构建一个二十一点游戏。因此,当银行达到 30 美元时,将进行另一轮游戏,然后游戏结束。

庄家的价值取决于玩家的下注(庄家的策略为 10 美元,如果玩家下注 3 并输掉该轮庄家则有 13 美元)

loops while-loop conditional-statements python-3.x

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

While 循环确认用户输入

我陷入了 ac# 问题,无法结束 while 循环。

userValue 是之前在程序中输入的字符串。

该字符串采用 DateTime 格式,新输入需要与原始 userValue 匹配。IE确认用户的原始出生日期。

我使用 try / catch 来确定输入是否可以解析为 DateTime。

目前这只是一个持续循环,我试图在验证用户出生日期后停止循环。

string userValueTwo;

int stop = 0;
while (stop != 0)
{
    Console.WriteLine("Please verify your birth date");
    userValueTwo = Console.ReadLine();

     try
     {
        DateTime birthdayTwo = DateTime.Parse(userValueTwo);
     }
     catch
     {
         Console.WriteLine("You did not enter a valid format.");
         Console.ReadLine();
     }

     if (userValueTwo == userValue)
     {
         Console.WriteLine("Birth date confirmed.");
         Console.ReadLine();
     }

    else 
    {
        Console.WriteLine("Your birthday did not match our records. Please try again"); …
Run Code Online (Sandbox Code Playgroud)

c# parsing try-catch while-loop

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

如何计算以字符串开头的单词数

我正在尝试编写一个计算前缀、后缀和根的代码。我需要知道的是如何计算以特定字符串(例如“co”)开头或结尾的单词数。

这就是我到目前为止所拥有的。

SWL=open('mediumWordList.txt').readlines()
  for x in SWL:
      x.lower
      if x.startswith('co'):
          a=x.count(x)
while a==True:
    a=+1
    print a
Run Code Online (Sandbox Code Playgroud)

我从中得到的只是一个无限循环。

python loops list while-loop python-2.7

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

为什么我的输入文件的最后一行运行了两次?

输入文件

Miller Andrew 65789.87 5
Green Sheila 75892.56 9
Sethi Amit 74900.50 6.1
Run Code Online (Sandbox Code Playgroud)
ifstream inFile;
ofstream outFile;
string laastName;
string firstName;
double salary;
double percent;
double new Salary;
double increase;

inFile.open("Ch3_Ex6Data.txt");
outFile.open("Ch3_Ex6Output.dat");

while(!inFile.eof()) {

    inFile >> lastName;
    inFile >> firstName;
    inFile >> salary;
    inFile >> percent;

    percent /= 100;
    increase = salary * percent;
    newSalary = increase + salary;

    outFile << firstName << " " << lastName << " ";
    outFile << setprecision(2) << fixed << newSalary << endl;

} …
Run Code Online (Sandbox Code Playgroud)

c++ file-io while-loop eof

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

如何用递归函数替换while?

我while以这种方式使用:

while (value < -180 || value > 180) {
  if (value < -180) {
    value += 360
  }
  if (value > 180) {
    value -= 360
  }
}
Run Code Online (Sandbox Code Playgroud)

不过,我想使用递归函数而不是while. 我在谷歌上搜索但找不到任何东西。所以我想也许我可以在这里得到答案。

javascript recursion while-loop

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

Python总是向下舍入?

我正在尝试完成一项作业,并且非常接近 python 总是将我的答案向下舍入,而不是在应该的时候向上舍入。这是我的代码:

startingValue = int(input())
RATE = float(input()) /100
TARGET = int(input())
currentValue = startingValue
years = 1

print("Year 0:",'$%s'%(str(int(startingValue)).strip() ))

while years <= TARGET :
  interest = currentValue * RATE
  currentValue += interest
  print ("Year %s:"%(str(years)),'$%s'%(str(int(currentValue)).strip()))
  years += 1
Run Code Online (Sandbox Code Playgroud)

这是我的代码输出:第0年:$10000,第1年:$10500,第2年:$11025,第3年:$11576,第4年:$12155,第5年:$ 12762, 第6年:$13400,第7年:$14071, 第8年: $14774, 9年级:$15513,

以下是应该输出的内容: Year 0: $10000, Year 1: $10500, Year 2: $11025, Year 3: $11576, Year 4: $12155, Year 5: $12763 , Year 6 : $13401 , Year 7: $14071, Year …

python rounding-error rounding while-loop

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

C 中带有变量定义的 while 循环

我目前正在尝试循环通过 C 中的 API 检索到的集合。

API 提供了一个函数 getNext(),它返回对集合中下一个元素的引用,如果已到达末尾,则返回 NULL。

现在我想在 while 表达式中声明一个变量并循环直到 NULL:

// create collection from API
Collection collection = create();

if (collection == NULL) {
  return -1
}

while (const Element *e = getNext(collection) != NULL) {
  // print data from e
}
Run Code Online (Sandbox Code Playgroud)

编译时,弹出“错误:意外的表达式”。为什么该语句在 C 中不起作用?当我只有一个返回元素或 NULL 的函数 getNext() 时,如何循环遍历集合?

我也尝试了以下操作,但出现了同样的错误:

while ((const Element *e = getNext(collection)) != NULL) {
  // print data from e
}
Run Code Online (Sandbox Code Playgroud)

c while-loop

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

Common Lisp 是否有 while True(即无限循环)的语法糖?

Common Lisp 的 LOOP 宏因其在循环方面无所不能而臭名昭著。尽管如此,while循环还是有点困难。的特殊情况while true,即无限循环,是一个例外吗?也就是说,Common Lisp 是否为无限循环提供任何语法糖?

我试图在网上找到类似的东西但失败了。本食谱建议创建一个循环列表,然后告诉 LOOP 宏对该无限列表中的每个元素执行某些操作。这显然是有效的,但它不是任何语法糖。作为比较,R 语言允许用户编写while(TRUE){...}为repeat{...}. 这就是我心目中的语法糖。

注意:尽管我很喜欢看到 Lisp 宏,但我并不要求任何人repeat在 Common Lisp 中实现。我想从一开始就知道该语言中已有的内容。

common-lisp infinite-loop while-loop

3
推荐指数
2
解决办法
752
查看次数

Nushell 中的 while/until 循环

作者注:此问题使用旧版本 Nushell 中过时的语法和语句。请参阅下面的答案以了解更新的语法。

Nushell 脚本中如何执行 while/until 循环?

由于 Nushell 有一个相当令人惊叹的表/JSON 解析系统,我一直在尝试通过它使用Stack Exchange API。

第一个挑战是循环访问 API 调用的多个可能的结果页面。我的(通常是程序性的,有时是面向对象的)背景让我在 Nushell 中找到了一个构造,例如:

let page = 1
let re = (http (echo "/2.3/questions?fromdate=1648771200&todate=1648944000&order=desc&sort=activity&site=askubuntu&page=" $page) | from json)
let questions = $re.items

while ($re.has_more) {
    let page = page + 1
    let re = (http (echo "/2.3/questions?fromdate=1648771200&todate=1648944000&order=desc&sort=activity&site=askubuntu&page=" $page) | from json)
    let questions = $questions | append $re.items
}
Run Code Online (Sandbox Code Playgroud)

...或等效的until构造。

我如何在 Nushell 中实现这一点?

注意 -在上面的示例中使用httpiegzip ,因为它会自动处理Stack API 所需的压缩( …

while-loop until-loop nushell

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

Kotlin:循环遍历下一个函数返回的有限值

背景信息

某些编程语言中的常见模式是有一个函数,在调用时返回下一个值,直到到达有限序列的末尾,在这种情况下,它会一直返回 null。

Java 中的一个常见示例是这样的:

void printAll(BufferedReader reader) {
    String line;

    // Assigns readLine value to line, and then check if not null
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
}
Run Code Online (Sandbox Code Playgroud)

它与Iterator 设计模式iterator中的 类似,但迭代器有 a和 a ,而BufferedReader没有检查功能,只有 形式,其中返回的对象可以为 null 以标记序列的结束。我调用诸如“下一个函数”(或者可能是“ yield ”函数)之类的函数,但我不知道是否有一个词来形容这种模式。next(): ObjecthasNext(): BooleanhasNext()next(): Object?next()

在 Java 中,表达式可以包含赋值,这允许如下构造:(line = reader.readLine()) != null。此代码将可为空的值赋给readLine()to line,然后检查 in 的值是否line不为空。但 Kotlin 不允许这样的构造,因为在 Kotlin 中,赋值不是表达式,因此它不能用作 …

iteration iterator while-loop bufferedreader kotlin

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