如何为此编写代码? [英] How do a write code for this?

查看:86
本文介绍了如何为此编写代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助,我是编码的新手,这是我的第一堂课,我现在坚持这个问题几个小时了。



写一个功能温度转换器( )按以下顺序采用以下参数:

1.值:表示温度的浮点数
2.缩放自:表示字符串的字符串温度标度表示的值;有效值

包括'F','C'和'K'(华氏温度,摄氏温度和开尔文温标)

3.缩放为:表示字母的字符串温度标度我们要将值转换为;有效值包括

'F','C'和'K'

简单地说,函数取值,这是在标识的温度刻度上表示的有效温度

按比例缩放,并使用温度转换公式表示温度等级上的值
比例。然后由函数返回转换后的浮点温度。



我尝试过:



我先尝试在纸上搞清楚,但我真的不知道从哪里开始这个

解决方案

这是你的作业,所以我不会给你任何代码!



但导师带领你走过这个非常温柔的地方:阅读你的课程笔记和/或书籍的相关页面如果你停止恐慌并仔细阅读这个问题,那么课程附带应该可以帮到你。

一次拿一点,这并不困难。

< blockquote class =quote>

引用:

写一个函数温度转换器()...

所以...你知道如何编写一个函数,是吗?而他告诉你该怎么称呼它。如果你不记得了,那么请看这里: Python函数 [ ^ ]

  def  temperatureconverter(...):
...



Quote:

...按以下顺序采用以下参数:

1. value:表示温度的浮点数



这很简单:

  def 温度转换器(temp,...) :
...

依此类推:您需要三个参数,并根据输入和输出温度标度转换少量代码。为了使代码更容易,我要做的是使用input scale参数将输入temp转换为Kelvin。然后我通过查看输出比例参数将其转换为所需的单位(JUst,因为这是看三个选项,两次的情况,而不是如果它是华氏度,我想要华氏度然后它没关系,否则如果它是华氏度,我想要摄氏度然后这样做,否则如果它是华氏度我希望开尔文做那个,否则如果它是摄氏度我想要华氏度所以另一个,否则如果......这只是永远持续。:笑:



试一试:编写函数大纲,只需返回与输入相同的值,然后添加逻辑以使函数执行应有的操作! / BLOCKQUOTE>

Please help, I am new to coding and this is my first class and I am stuck on this problem for hours now.

Write a function temperature converter() that takes the following arguments, in this order:
1. value: a floating-point number that represents a temperature
2. scale from: a string that represents the temperature scale that value is expressed in; valid values
include ’F’, ’C’ and ’K’ (for Fahrenheit, Celsius and Kelvin temperature scales)
3. scale to: a string that represents the temperature scale we want to convert value to; valid values include
’F’, ’C’ and ’K’
Simply put, the function takes value, which is a valid temperature expressed on the temperature scale identified
by scale from, and uses a temperature conversion formula to express value on the scale to temperature
scale. The converted, floating-point temperature is then returned by the function.

What I have tried:

I have tried figuring it out on paper first but really i just have no idea where to start with this

解决方案

This is your homework, so I'll give you no code!

But the tutor is leading you through this pretty gently: reading your course notes and / or the relevant pages of the books that accompany the course should get you through if you stop panicking and read the question carefully.
Take it one bit at a time, and it isn't difficult.

Quote:

Write a function temperature converter() ...

So ... you know how to write a function, yes? And he's told you what to call it. If you can't remember, then look here: Python Functions[^]

def temperatureconverter (...):
   ...


Quote:

...that takes the following arguments, in this order:
1. value: a floating-point number that represents a temperature


That's easy:

def temperature converter (temp, ...):
   ...

And so on: you need three parameters, and a small amount of code to convert depending on the input and output temperature scales. To make the code easier, what I'd do is convert the input temp to Kelvin using the "input scale" parameter. Then I'd convert it to the required unit by looking at the "output scale" parameter (JUst because that's a case of "look at the three options, twice" instead of "if it's fahrenheit and I want fahrenheit then it's ok, else if it's fahrenheit and I want celsius then do this, else if it's fahrenheit and I want kelvin do that, else if it's celsius and I want fahrenheit so the other, else if ..." which just goes on forever. :laugh:

Give it a try: get the function outline written and working just returning the same value as you input, then add the logic to get the function doing what it should!


这篇关于如何为此编写代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆