【问题标题】:while loop back to start of programwhile 循环回到程序的开头
【发布时间】:2017-03-09 07:17:43
【问题描述】:

我正在尝试创建一个循环以使用户回到程序的开头。我无法让它打印“欢迎使用比较功能”。程序将运行,要求用户输入 1 和 2,然后打印比较的答案,但我不知道如何让它重新开始。

def comparison():
    loop = True
    while (loop):
        print(" Welcome to the comparison function")
    a = int (input("Enter your first number for value a, then hit enter key:"))
    b = int (input("Enter your second number for value b, then hit enter key:"))
    def compare (a,b):
        if a>b:
            return 1
        elif a==b:
            return 0
        else:
            return -1
    answer = compare(a,b)
    print (answer)
    while True:
    response=input ("Would you like to perform another comparison? (yes/no)")
    if response=="yes" or response =="YES" or response =="Yes":
        print ("Thank you!")
        break
    elif response=="no" or response=="NO" or response =="No":
        loop=False
        print ("Thank you, have a great day!")
        break
    else:
        continue

【问题讨论】:

  • 显示的代码会出现缩进错误。
  • 是的,请编辑您的帖子,使其具有正确的缩进,因为它显示在您的代码中。
  • 此代码在正确缩进时工作正常......所以你的问题是缩进。
  • 假设您的代码中的缩进很好(否则它根本不会为您运行),您使用的是什么版本的 Python?我可以让它在 Python 2 中正常运行,但它需要将 input 更改为 raw_input
  • 我现在看到的代码在 Python 3.5 中运行得很好

标签: python while-loop


【解决方案1】:

这将替代您在 Python 3 中的函数:

def comparison():
    while True:
        print("Welcome to the comparison function")
        a = int(input("Enter your first number for value a, then hit enter key:"))
        b = int(input("Enter your second number for value b, then hit enter key:"))

        # Recommended replacement for cmp(a, b) in Python 3
        # https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons
        answer = (a > b) - (a < b) 
        print(answer)

        response = input("Would you like to perform another comparison? (yes/no)")
        while response.lower() not in ['yes', 'no']:
            response = input("please enter proper response: ")

        if response.lower() == "yes":
            print("Thank you!")
        else:
            print("Thank you, have a great day!")
            break

comparison()

【讨论】:

    【解决方案2】:
    def comparision():
        loop = True
        while loop:
                print ("welcome to the comparision function: ")
                a = int(input(("Enter your number for a value a , then hit enter key:")))
                b = int (input("Enter your second number for value b, then hit enter key:"))
                answer = ''
                if a > b:
                    answer = 1
                elif a == b:
                    answer = 0
                else:
                    answer = -1
                print (answer)
                response = str(input("Would you like to perform another comparison? (yes/no) :"))
    
                while response.lower() not in ['yes', 'no']:
                    response = str(input("please enter proper response: "))
    
                if response.lower() == 'yes'
                    continue
                elif response.lower() == 'no' 
                    print ("Thank you, have a great day!")
                    break
    
    if __name__ == '__main__':
        comparision()
    

    如果您使用的是 python 2.7:

    response = str(raw_input("Would you like to perform another comparison? (yes/no) :"))
    

    希望这会有所帮助。谢谢

    【讨论】:

    • 您还删除了用于选择是否再去的内部循环......这不是一个功能等效的程序。
    • 对不起,我把代码粘贴在这里不好。已编辑@roganjosh
    • @TemporalWolf 我们这里需要内循环吗?
    • 非常感谢您的帮助!你能解释一下你做了什么改变,这样我下次就不会犯同样的错误了吗? @Bhargav
    • 我删除了内部函数 compare 并删除了一个,同时 True 条件进行优化。如果它对你有用,请标记答案,请寻找缩进@StacyB。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2018-08-22
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多