2014-10-18 66 views
-2

這是我的代碼:的Python 3.4.1 - 把變量放入輸入

import random 
x = random.randint(2,10) 
y = random.randint(2,10) 
answer = int(input("How much is x * y?")) 

我有我的輸入使用變量x, y,因此用戶會看到這個當它們運行的​​程序(該程序顯然是沒有完成,我只需要這個幫助):

How much is 3 * 5? 

但我不知道如何把這些變量內的輸入..請幫助!

回答

2

使用format

import random 
x = random.randint(2, 10) 
y = random.randint(2, 10) 
answer = int(input("How much is {} * {}?".format(x, y))) 

儘量不要使用%操作。 format是優先於此,尤其是因爲蟒蛇3.

0

它的simple string formatting問題:

"How much is %d * %d?" % (x, y) 
+0

應該使用.format而不是舊的%運算符 – 2014-10-18 15:59:43