添加python循环最佳实践

时间:2018-12-06 17:47:56

标签: python python-3.x

我是python的新手,正在尝试学习学习编码的最佳方法。我正在用一些简单的代码进行练习,该代码要求2个数字并将它们乘以2,并且我想添加一个函数来询问您是否已完成或是否要乘以更多数字。下面是我开始的代码。我想知道让它循环返回再问两个数字是还是否的最佳方法是什么?

    #Ask for number to be multiplied
    num1 = input("\nEnter the first number you want to multiply: ").strip()
    num2 = input("\nEnter the second number you want to multiply: ").strip()

    #Convert input to interger
    num1 = int(num1)
    num2 = int(num2)

    #Multiply the numbers
    results = num1 * num2

    #Print the results
    print("Your answer is: " + str(results))

5 个答案:

答案 0 :(得分:0)

其中循环是最常用的循环:

repeat = 'yes'
while repeat.lower() == 'yes':
    #Ask for number to be multiplied
    num1 = input("\nEnter the first number you want to multiply: ").strip()
    num2 = input("\nEnter the second number you want to multiply: ").strip()

    #Convert input to interger
    num1 = int(num1)
    num2 = int(num2)

    #Multiply the numbers
    results = num1 * num2

    #Print the results
    print("Your answer is: " + str(results))
    print('If you want to continue type yes or no')
    repeat = input()

答案 1 :(得分:0)

您可以通过将整个代码包装在while True循环中并使用break语句退出来实现。本质上,我们将要永远重复该过程,直到用户键入nN。可以根据需要完善检查条件。

while True:
    # Ask for number to be multiplied
    num1 = input("\nEnter the first number you want to multiply: ").strip()
    num2 = input("\nEnter the second number you want to multiply: ").strip()

    # Convert input to interger
    num1 = int(num1)
    num2 = int(num2)

    # Multiply the numbers
    results = num1 * num2

    # Print the results
    print("Your answer is: " + str(results))

    # Ask to continue or not
    res = input("\nWould you like to continue? (y/n) ").strip()
    if res.lower() == 'n':
        break

答案 2 :(得分:0)

您可以设置一个提醒,例如当用户完成操作时,使您可以使提醒是False以使其完成,或进行input并检查用户是否要exit然后只需打破循环即可。

# set reminder for while loop
done = False

while not done:
    #Ask for number to be multiplied
    num1 = input("\nEnter the first number you want to multiply: ").strip()
    num2 = input("\nEnter the second number you want to multiply: ").strip()

    #Convert input to interger
    num1 = int(num1)
    num2 = int(num2)

    #Multiply the numbers
    results = num1 * num2

    #Print the results
    print("Your answer is: " + str(results))
    ask = input('Do you want to try again y/n: ')
    if ask == 'n':
        done = True # set the reminder is true or break the loop
        # break

答案 3 :(得分:0)

只需简单地使用while循环并声明一个名为choice的变量,然后要求用户输入选择即可。

while(True):
    choice = input("Would you like to continue? (y/n) ")
    if(choice.lower=='y'):
        num1 = int(input("\nEnter the first number you want to multiply: "))
        num2 = int(input("\nEnter the second number you want to multiply: "))
        results = num1 * num2
        print("Your answer is: ",results)
    else:
        break

答案 4 :(得分:0)

def termination():
    _noexit=str(raw_input())


    if _noexit.lower() == 'y':
        return True
    else:
        return False    


#Cal the termination condition
while termination:

    num1 = input("\nEnter the first number you want to multiply: ")
    num2 = input("\nEnter the second number you want to multiply: ")


    num1 = int(num1)
    num2 = int(num2)

    results = num1 * num2
    print results

    #Checking termination
    termination()