Python 3-温度转换器

时间:2018-10-15 18:20:45

标签: python-3.x

我对任何编程都是新手,我试图在python中构建温度转换器函数,但我不断出错。 我希望该函数首先提示用户输入华氏温度并将其转换为摄氏温度;然后提示用户输入摄氏温度并将其转换为华氏温度。下面是我的代码。任何帮助将不胜感激

def main():
    F = input("Input the temperature in Fahrenheit: ")
    C = (5 / 9) * (float(F - 32))
    print(int(C))

    input("input the temperature in Celsius")
    F = 9/5 * (float(C + 32))
    Print(int(F))

main() 

这是我收到的错误消息:

Traceback (most recent call last):
  File "/Users/mabook/Library/Preferences/PyCharmCE2018.2/scratches/scratch_7.py", line 11, in <module>
    main()
  File "/Users/mabook/Library/Preferences/PyCharmCE2018.2/scratches/scratch_7.py", line 4, in main
    C = (5 / 9) * (float(F - 32))
TypeError: unsupported operand type(s) for -: 'str' and 'int'

1 个答案:

答案 0 :(得分:1)

注释中所述错误的原因是,您不能从整数中获取字符串。因此,您需要将输入转换为可以从整数(例如浮点数)中删除的某种类型,但是在执行计算之前必须执行此操作。

要使此代码正常工作,还需要更改其他内容:

  • I/result: <||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b};google.https=function(){return"https:"==window.location.protocol};google.ml=function(){return null};google.log=function(a,b,e,c,g){if(a=google.logUrl(a,b,e,c,g)){b=new Image;var d=google.lc,f=google.li;d[f]=b;b.onerror=b.onload=b.onabort=function(){delete d[f]};google.vel&&google.vel.lu&&google.vel.lu(a);b.src=a;google.li=f+1}};google.logUrl=function(a,b,e,c,g){var d="",f=google.ls||"";e||-1!=b.search("&ei=")||(d="&ei="+google.getEI(c),-1==b.search("&lei=")&&(c=google.getLEI(c))&&(d+="&lei="+c));c="";!e&&google.cshid&&-1==b.search("&cshid=")&&"slh"!=a&&(c="&cshid="+google.cshid);a=e||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+d+f+"&zx="+google.time()+c;/^http:/i.test(a)&&google.https()&&(google.ml(Error("a"),!1,{src:a,glmm:1}),a="");return a};}).call(this);(function(){google.y={};google.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1};google.lm=[];google.plm=function(a){google.lm.push.apply(google.lm,a)};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};}).call(this);google.f={};(function(){google.hs={h:true};})();(function(){google.c={c:{a:true,m:false,n:false,p:false,wi:true}};google.sn='webhp';(function(){var e=function(a,b,c){a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent&&a.attachEvent("on"+b,c)};google.tick=function(a,b,c){google.timers[a]||google.startTick(a);c=void 0!==c?c:google.time();b instanceof Array||(b=[b]);for(var d=0;d<b.length;++d)google.timers[a].t[b[d].clearcut]={key:b[d],ts:c}};google.c.e=function(a,b,c){google.timers[a].e[b]=c};google.c.b=function(a){var b=google.timers.load.m;b[a]&&google.ml(Error("a"),!1,{m:a});b[a]=!0};google.c.u=function(a){var b=google.timers.load.m;if(b[a]){b[a]=!1;for(a in b)if(b[a])return;google.csiReport()}else google.ml(Error("b"),!1,{m:a})};google.rll=function(a,b,c){var d=function(b){c(b);b=d;a.addEventListener?a.removeEventListener("load",b,!1):a.attachEvent&&a.detachEvent("onload",b);b=d;a.addEventListener?a.removeEventListener("error",b,!1):a.attachEvent&&a.detachEvent("onerror",b)};e(a,"load",d);b&&e(a,"error",d)};google.afte=!0;google.aft=function(a){google.c.c.a&&google.afte&&(google.timers.aft||google.startTick("aft"),google.timers.aft.t[a.id||a.src||a.name]=google.time())};google.c.b("pr");google.c.b("xe");}).call(this);})();(function(){var b={gen204:"dcl",clearcut:4};var c=[function(){google.c&&google.tick("load",b)}];google.dclc=function(a){c.length?c.push(a):a()};function d(){for(var a;a=c.shift();)a()}window.addEventListener?(document.addEventListener("DOMContentLoaded",d,!1),window.addEventListener("load",d,!1)):window.attachEvent&&window.attachEvent("onload",d);}).call(this);(function(){var b=[];google.jsc={xx:b,x:function(a){b.push(a)},mm:[],m:function(a){google.jsc.mm=a},aa:[],a:function(a){google.jsc.aa.push(a)},dd:[],d:function(a){google.jsc.dd.push(a)}};}).call(this);(function(){var g=this,h=Date.now||function(){return+new Date};var y={};var A=function(a,d){return function(b){b||(b=window.event);return d.call(a,b)}},B="undefined"!=typeof navigator&&/Macintosh/.test(navigator.userAgent),C=function(){this._mouseEventsPrevented=!0};var E=function(){this.v=[];this.g=[];this.h=[];this.s={};this.i=null;this.l=[];D(this,"_custom")},G="undefined"!=typeof navigator&&/iPhone|iPad|iPod/.test(navigator.userAgent),H=String.prototype.trim?function(a){return a.trim()}:function(a){return a.replace(/^\s+/,"").replace(/\s+$/,"")},I=/\s*;\s*/,M=function(a,d){return function(b){var c=d;if("_custom"==c){c=b.detail;if(!c||!c._type)return;c=c._type}var e;var f;"click"==c&&(B&&b.metaKey||!B&&b.ctrlKey||2==b.which||null==b.which&&4==b.button||b.shiftKey)&& 应该为小写
  • 第二个输入需要存储在变量中,以便我们可以使用它
  • 转换为华氏温度时需要更改方括号以使公式正确
  • 除非您有必要将数字截断为int并删除小数点后的所有内容,否则我们也可以按原样打印结果。如果确实需要将结果作为整数,则应考虑四舍五入到最接近的整数,而不仅是舍入值

所有这些更改后,代码可能看起来像这样:

print