我用 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'