Python:如何在输入提示中调用变量?

时间:2017-02-08 04:14:14

标签: python-3.x

我想问以下问题:

  

球队名称是什么?

     

(team_name)得分多少分?

我已经尝试了这个,但又收到了错误:

team_name = input("What is the team name? ")

points = input("How many points did", team_name, "score? ")

请帮忙!

4 个答案:

答案 0 :(得分:1)

你得到什么错误?

我相当自信你的问题是你在第二行输入了三个参数,而不是连接我认为你在这里尝试做的字符串。

尝试:

points = input("How many points did " + str(team_name) + " score?")

答案 1 :(得分:1)

这是一种python3.6方式:

points = input(f"How many points did {team_name} score?")

答案 2 :(得分:1)

team_name = input("What is the team name:")
points = "How many points did %s score" #%s is the variable
points_print = input(points % (team_name))

这是解决问题的简便方法,使用'%s'调用变量,打印后看起来会更好。

答案 3 :(得分:0)

for a in range(1,1000):
    for b in range(1,1000):
        for c in range(1,1000):
            if b>a and c and c>b:
                if c**2==b**2+a**2:
                    print((a,b,c))

输出:

(3, 4, 5)
(5, 12, 13)
(6, 8, 10)
(7, 24, 25)
(8, 15, 17)
(9, 12, 15)
(9, 40, 41)
(10, 24, 26)
(11, 60, 61)
(12, 16, 20)
(12, 35, 37)
(13, 84, 85)
(14, 48, 50)
(15, 20, 25)
(15, 36, 39)
(15, 112, 113)
... (goes on)