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 个回答
0
这其实是一个关于简单字符串格式化的问题:
"How much is %d * %d?" % (x, y)
2
使用 format
方法:
import random
x = random.randint(2, 10)
y = random.randint(2, 10)
answer = int(input("How much is {} * {}?".format(x, y)))
尽量不要使用 %
这个符号来格式化字符串。从 Python 3 开始,使用 format
方法更好。