クラスでは、華氏を摂氏に、またはその逆に変換するプログラムを作成します。結果は小数点第1位に四捨五入する必要があります。「ラウンド」機能をさまざまな方法で試しましたが、成功しませんでした。
temp=float(input("Enter a temperature value to convert: "))
unit=str(input("Convert to Fahrenheit or Celsius? Enter f or c: "))
if unit=="c" or unit == "C":
degC=(temp)
temp= (1.8*temp)+32
print(str(temp) + " degrees fahrenheit = " + str(degC) + " degrees Celsius. ")
if unit=="f" or unit == "F":
degF=(temp)
temp= (temp-32)/1.8
print(str(temp)+ " degrees celsius = " + str(degF) + " degrees Fahrenheit. ")
else:
print("you did not enter an f or c. Goodbye ")
round(number, 1)
内蔵機能が使えます!
例えば:
>>> round(45.32345, 1)
45.3
あなたの場合:
temp=float(input("Enter a temperature value to convert: "))
unit=str(input("Convert to Fahrenheit or Celsius? Enter f or c: "))
if unit=="c" or unit == "C":
degC=(temp)
temp= (1.8*temp)+32
print(str(round(temp), 1) + " degrees fahrenheit = " + str(degC) + " degrees Celsius. ")
el*emphasized text*if unit=="f" or unit == "F":
degF=(temp)
temp= (temp-32)/1.8
print(str(round(temp), 1)+ " degrees celsius = " + str(degF) + " degrees Fahrenheit. ")
else:
print("you did not enter an f or c. Goodbye ")
Pythonが実際に行うことは、次のようなものです。
def truncate(n, decimals=0):
multiplier = 10 ** decimals
return int(n * multiplier) / multiplier
Pythonの丸めについてもっと読むことができます
お役に立てれば!
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加