2014-10-18 64 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