plapapyjh 2022-09-12 06:39 采纳率: 43.5%
浏览 16
已结题

Python初学者基础问题

这个代码就是一个摄氏度和华摄氏度相互转换的代码,代码没问题,如何将摄氏度和华摄氏度转换部分分别定义为两个函数,来运行,麻烦看一下,给一个修改后的代码,谢谢

print("Press1:Fahrenheit→centigrade")
print("Press2:centigrade→Fahrenheit")

temp=input("Please enter the function you want to select:")
guess=int(temp)

if guess==1 :
    temp=input("Please enter the Fahrenheit to be converted:")
    F=int(temp)
    C=(F-32)/1.8
    print("Fahrenheit:"+str(F)+"F = centigrade:"+str(C)+"℃")
elif guess==2 :
    temp=input("Please enter the centigrade to be converted:")
    C=int(temp)
    F=C*1.8+32
    print("centigrade:"+str(C)+"℃ = Fahrenheit:"+str(F)+"F")
else :
     print("Please check your input!")

  • 写回答

2条回答 默认 最新

  • 梦里逆天 2022-09-12 07:01
    关注
    
    print("Press1:Fahrenheit→centigrade")
    print("Press2:centigrade→Fahrenheit")
    
    temp = input("Please enter the function you want to select:")
    guess = int(temp)
    
    
    def convertFah_Centi(F):
        C = (F - 32) / 1.8
        print("Fahrenheit:" + str(F) + "F = centigrade:" + str(C) + "℃")
    
    
    def convertCenti_Fah(C):
        F = C * 1.8 + 32
        print("centigrade:" + str(C) + "℃ = Fahrenheit:" + str(F) + "F")
    
    
    if guess == 1:
        temp = input("Please enter the Fahrenheit to be converted:")
        F = int(temp)
        # C = (F - 32) / 1.8
        # print("Fahrenheit:" + str(F) + "F = centigrade:" + str(C) + "℃")
        convertFah_Centi(F)
    elif guess == 2:
        temp = input("Please enter the centigrade to be converted:")
        C = int(temp)
        # F = C * 1.8 + 32
        # print("centigrade:" + str(C) + "℃ = Fahrenheit:" + str(F) + "F")
        convertCenti_Fah(C)
    else:
        print("Please check your input!")
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 9月19日
  • 已采纳回答 9月12日
  • 创建了问题 9月12日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部