You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何在print语句同一行调用函数输出返回值

问题原因

你当前的代码存在两个问题,导致无法实现预期输出效果:

  1. 原生print函数默认会在输出内容末尾添加换行符,冒号后的内容会自动跳到下一行
  2. 调用multiply()后仅生成了返回值,没有将返回值传入打印语句,计算结果根本不会输出到控制台

可行修改方案

以下三种方案任选其一,替换原代码中最后两行即可:

  • 方案1(最推荐,语法清晰简洁):使用f-string直接嵌入计算结果
print(f"the product of the two integers is: {multiply()}")
  • 方案2:利用print多参数自动拼接的特性
print("the product of the two integers is: ", multiply())
  • 方案3:保留原有两行结构,取消print默认换行符
# 用end参数把默认的换行符改为空字符串
print("the product of the two integers is: ", end="")
# 直接打印函数返回值,会跟在上一行内容后面
print(multiply())

额外优化提示

你现有代码还有两处小问题可以调整:

  1. 第二个输入框的提示文案写错了,仍然是Enter the first number you would like to multiply: ,建议修改为Enter the second number you would like to multiply:
  2. 如果你需要用户输入的是整数,建议把float(input(...))改为int(input(...)),避免出现小数结果不符合作业要求

内容的提问来源于stack exchange,提问作者Fuji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 23:57:00