如何在python函数中组合变量和字符串?

时间:2017-04-13 10:02:08

标签: python function

我正在尝试使用Python构建一些简单的函数,我使用的是Jupyter Qt控制台。 这是我的功能:

def fishmarket(price):
    print("How much is the fish?")
    print("So you say the fish costs" % price "?")   
    if price > 5:
            print("That's too much!")
    else: print("That's fine by me")
    print("Have a nice day, sir!")

我希望人们调用该功能并定义价格,然后根据价格得到答案。我在问号周围出现语法错误。如何在单个打印输出中组合字符串和变量price?我已经从另一个答案中学到了%应该使用数值变量来解决这个问题,但它并没有帮助。

2 个答案:

答案 0 :(得分:-1)

使用格式:

print("You say the price is {}".format(price))

答案 1 :(得分:-1)

你可以试试这个

def fishmarket(price):
  print("How much is the fish?")
  print("So you say the fish costs", price, "?")
    if price > 5:
  print("That's too much!")
  else: print("That's fine by me")
  print("Have a nice day, sir!")

def fishmarket(price):
      print("How much is the fish?")
      print("So you say the fish costs {} ?".format(price))
        if price > 5:
      print("That's too much!")
      else: print("That's fine by me")
      print("Have a nice day, sir!")