如何在python中无限循环这个程序



好吧,我刚刚开始学习python,不知道在哪里可以提出这个问题。我写了这个计算器,它很管用。

a,b=input("Enter two numbers (sperated by a comma): ")
operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
if operator=="divide":
    print "Your answer is", a / b
if operator=="multiply":
    print "Your answer is", a * b
if operator=="add":
    print "Your answer is", a + b
if operator=="subtract":
    print "Your answer is", a - b    
repeat=raw_input("Do you want to perform another calculation(yes or no)? ")

现在,我想知道如何重新启动这个程序,如果输入是肯定的。我寻找答案,但我找不出循环整个过程的命令。就像while循环一样,你必须在条件之后给出一个命令,但这个命令是什么?ples halp。

将其包裹在while循环中:

repeat = "yes"
while repeat.lower().strip() == 'yes':
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b    
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")

只要答案是"yes"(不区分大小写),此操作就会循环。初始的"repeat"是为了保证循环至少运行一次。

顺便说一下,你可以把你的if测试变成一个dict:

import operator
ops = {'divide': operator.div,
       'multiply': operator.mul,
       'add': operator.add,
       'subtract': operator.sub}
repeat = "yes"
while repeat.lower().strip() == 'yes':
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ").lower().strip()
    if operator not in ops:
        print 'Invalid operator:', operator
        continue
    print "Your answer is", ops[operator](a,b)
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
while True:
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b    
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
    if repeat == 'no':
        break

当True永远继续循环时,它可以被关键字break打断(它从最近的循环中打断(for或While))。

当处理用户的输入以确定条件时,最好检查用户的输入是以"y"(是)开头还是以"n"(否)开头,因为用户可能会输入y而不是"是",他可能会混淆大写,这里有一个代码可以实现这一点:

while True:
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b    
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
    if repeat.lower.startswith('n'):
        break 

"repeat"是一个字符串,它有一个名为"lower"的方法(类似于函数),该方法返回字符串的小写表示形式,然后您可以使用另一个称为startswith的方法来检查该小写表示形式(也是字符串)是否以字母"n"开头。

试试这个:

while True:    
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b    
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
    if repeat == "no":
        break
试试这个
您可以使用while True:,在for循环中编写的代码
将无限执行,
要停止执行,您可以使用Ctrl+C
while True:
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")     if repeat == "no":         break
for x in repeat.upper():
   a,b=input("Enter two numbers (sperated by a comma): ")
   operator=raw_input("Do you want to add, subtract, multiply, or 
   divide? ")
   if operator=="divide":
      print "Your answer is", a / b
   if operator=="multiply":
      print "Your answer is", a * b
   if operator=="add":
      print "Your answer is", a + b
   if operator=="subtract":
      print "Your answer is", a - b    
   if oper == repeat:
      break

它将循环直到特定条件为真

最新更新