ユーザーの体重/身長を入力として受け取るプログラムを Ruby で作成しました。私はそれをPythonに変換するのに行き詰まっています。これが正常に動作する私の Ruby コードです。
print "How tall are you?"
height = gets.chomp()
if height.include? "centimeters"
#truncates everything but numbers and changes the user's input to an integer
height = height.gsub(/[^0-9]/,"").to_i / 2.54
else
height = height
end
print "How much do you weigh?"
weight = gets.chomp()
if weight.include? "kilograms"
weight = weight.gsub(/[^0-9]/,"").to_i * 2.2
else
weight = weight
end
puts "So, you're #{height} inches tall and #{weight} pounds heavy."
これをどのように翻訳できるかについてのヒントや指針はありますか? ここに私のPythonコードがあります:
print "How tall are you?",
height = raw_input()
if height.find("centimeters" or "cm")
height = int(height) / 2.54
else
height = height
print "How much do you weight?",
weight = raw_input()
if weight.find("kilograms" or "kg")
weight = int(height) * 2.2
else
weight = weight
print "So, you're %r inches tall and %r pounds heavy." %(height, weight)
実行されていません。ここに私が得ているエラーがあります:
MacBook-Air:Python bdeely$ python ex11.py
How old are you? 23
How tall are you? 190cm
Traceback (most recent call last):
File "ex11.py", line 10, in <module>
height = int(height) / 2.54
ValueError: invalid literal for int() with base 10: '190cm'