插入停止声明

时间:2016-09-06 21:20:40

标签: python

如果输入数字 13 ,我怎样才能让这个程序停止?

print "\t:-- Enter a Multiplication --: \n"
x = ("Enter the first number: ")                                    
y = ("Enter your second number to multiply by: ")

for total in range(1, 12):
        x = input("Enter a number: ")
        y = input("Multiplied by this: ")
        print "\n  TOTAL: "
        print x, "X", y, "=", (x * y)


#Exits the program.
raw_input("\t\tPress Enter to Exit")

2 个答案:

答案 0 :(得分:0)

如果我理解你在这里要做什么,我认为if语句会是一个更好的解决方案。像这样:

print("Enter a multiplication")
x = int(input("Enter a number: "))
y = int(input("Multiplied by this: "))


def multiply(x, y):
    if 1 < x < 13 and 1 < y < 13:
        answer = x * y

        print("Total:")
        print("%s X %s = %s" % (x, y, answer))
    else:
        print("Your number is out of range")

multiply(x, y)

但说实话,你的代码中有很多部分可以运行。

答案 1 :(得分:0)

您使用进行循环;如果您在进入循环之前知道 要执行它多少次,这是合适的。这不是你的问题。相反,使用 while 循环;这种情况一直持续到特定情况发生。

尝试这样的事情:

# Get the first input:
x = input("Enter a number (13 to quit): ")
# Check NOW to see whether we have to quit
while x <= 12:
    # As long as the first number is acceptable,
    #   get the second and print the product.
    y = input("Multiplied by this: ")
    print "\n  TOTAL: \n", x, "X", y, "=", (x * y)

    # Get another 'x' value before going back to the top.
    x = input("Enter a number (13 to quit): ")

# -- Here, you're done with doing products.
# -- Continue as you wish, such as printing a "times table".