English 中文(简体)
如果发言没有像我预期的那样打破僵局
原标题:If statement doesn t break out of loop as I would expect
  • 时间:2024-03-10 02:10:17
  •  标签:
  • python

这是我迄今为止的职能守则(如果出现错误的话):

def showAddition():
    print("You have selected: "Addition"")
            
    while True:
        last_answer = None
        if last_answer:
            next_num = int(input("Please input another number: "))
            answer = calc.addNums(last_answer, next_num)
        else:
            num1 = int(input("Please enter your first number: "))
            num2 = int(input("Please enter your second number: "))
            answer = calc.addNums(num1, num2)
                
            print(f Your answer is {answer} )
            continue_prompt = input("Would you like to continue? Y/N
")
                    
            if continue_prompt.upper() == "Y" or continue_prompt.upper() == "YES":
                last_answer = answer
            else:
                break

我正试图这样做,以便我能够立即进入另一个编号,在数字后增加数字,而不会失败。 然而,出于某种原因,这 lo只是重复。 即便我对“不”的继续迅速投入,它甚至没有中断。 “赞成”和“反对”都产生了完全相同的结果,而没有结果。

问题回答
def showAddition():
    print("You have selected: "Addition"")
    
    # store the result of the previous addition
    last_answer = None
    # to stop the loop
    continue_adding = True
    
    while continue_adding:
        if last_answer:
            next_num = int(input("Please input another number: "))
            # answer = last_answer + next_num
            answer = calc.addNums(last_answer, next_num)
            print(f Your answer is {answer} )
            # store new value in last answer
            last_answer = answer
        else:
            num1 = int(input("Please enter your first number: "))
            num2 = int(input("Please enter your second number: "))
            # answer = num1 + num2 
            answer = calc.addNums(num1, num2)
                
            print(f Your answer is {answer} )
            
            # check input
            while True:
                continue_prompt = input("Would you like to continue? Y/N
")
                # continue addition directly with stored last_answer
                if continue_prompt.upper() in {"Y","YES"}:
                    last_answer = answer
                    break
                elif continue_prompt.upper() in {"N","NO"}:
                    # stop the loop
                    continue_adding = False
                    break
                else:
                    # ask again to only input Y/N
                    print("Only Enter Either Y/N")
                    continue
  1. store the last answer into the variable above while loop so it won t reset
  2. set loop variable continue_loop used to stop while loop without over coding the inputs if it s not Y/N
  3. Instead of using if x == y use set or list look ups: if x in [x,y,z]

除了Razumov关于纠正这一缺陷的建议外,注意到“投入另一号”组将永远不会运行,因为last_answer<>code>在每次停机时都会被重新定位为零。 只字不提......

如果你打算报告整整整整整整整整段的累积数额,就会尝试:

class calc:
    @staticmethod
    def addNums(*args):
        return sum(args)


def showAddition():
    print("You have selected: "Addition"")

    last_answer = None
    while True:
        if last_answer:
                        next_num = int(input("Please input another number: "))
            answer = calc.addNums(last_answer, next_num)
        else:
            num1 = int(input("Please enter your first number: "))
            num2 = int(input("Please enter your second number: "))
            answer = calc.addNums(num1, num2)

        print(f Your answer is {answer} )
        continue_prompt = input("Would you like to continue? Y/N
")

        if continue_prompt.upper() == "Y" or continue_prompt.upper() == "YES":
            last_answer = answer
        else:
            break

(You没有提供calc.addNums的实施,我补充说,在执行过程中,我可以操作该守则。)

你们是否尝试使用 b变,而不是打破2级?

i.e.

def showAddition():
    print("You have selected: "Addition"")
        
    continueProcess = True    
    while continueProcess:
        last_answer = None
        if last_answer:
            next_num = int(input("Please input another number: "))
            answer = calc.addNums(last_answer, next_num)
        else:
            num1 = int(input("Please enter your first number: "))
            num2 = int(input("Please enter your second number: "))
            answer = calc.addNums(num1, num2)
                
            print(f Your answer is {answer} )
            continue_prompt = input("Would you like to continue? Y/N
")
                    
            if continue_prompt.upper() == "Y" or continue_prompt.upper() == "YES":
                last_answer = answer
            else:
                continueProcess = False




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...