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

Python函数嵌套调用输入环节出现None输出的问题求助

问题原因

你代码里的input(print("提示语"))写法存在问题——print()函数执行后没有返回值(默认返回None),input()会把这个None当作提示文本的一部分输出,这就是你看到None的根源。

解决方案

把提示语直接作为input()的参数传入即可,input()本身支持直接传入提示字符串,不需要嵌套print()。同时还要修正代码里的类型转换问题(输入的内容默认是字符串,需要转为数值类型才能参与运算):

修改后的完整代码:

def func1(f_temp):
    c_temp = round((float(f_temp) - 32) * 5 / 9, 1)
    print(f"{f_temp} degrees Fahrenheit is equal to {c_temp} degrees Celsius.")

def func2(mph):
    mps = float(mph) * 0.44704
    print(f"{mph} miles per hour is equal to {mps} meters per second.")

def main():
    print("Enter 1 to convert Fahrenheit temperature to Celsius.")
    print("Enter 2 to convert speed from miles per hour to meters per second.")
    main_input = int(input())
    if main_input == 1:
        f_temp = input("Please enter a temperature in Fahrenheit: ")
        func1(f_temp)
    elif main_input == 2:
        mph = input("Please enter a speed in miles per hour: ")
        func2(mph)
    else:
        print("Please enter 1 or 2 only.")

main()

运行修正后的代码,就不会再出现None的输出,同时也能避免因类型不匹配导致的运算错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 05:05:18