2016-09-06 99 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".