2016-09-06 51 views
-2

如何在输入号码时停止该程序?插入停止声明

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") 
+1

英语:什么是你的问题?我不明白。 –

+0

我想在输入数字13后停止程序。 – Will

+1

@将在输入第13个数字或输入数字“13”后想要停止? – FamousJameous

回答

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) 

但说实话,你的代码有一些部分有些工作。

+0

美丽,我太亲近了!非常感谢 – Will

+0

其实它没有真正的,它必须保持一个声明.. – Will

0

您使用了作为循环;当你进入循环之前你知道你想要执行多少次这是适当的。这不是你的问题。相反,使用循环;这一直持续下去,直到发生特定情况。

尝试这样:

# 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".