2017-02-16 26 views
2

我想你是多少岁转化为天:时遇到一个代码

print "age into days converter" 
name = raw_input("What is your name: ") 
age = raw_input("How old are you: ") 
days_in_years = 365 
age_in_days = age * days_in_years 
print "You are %s days old" %age_in_days 

但是,它打印你的年龄,而不是365次相乘的。我试着用输入,INT(, 并试图将其转换为浮点值,但它仍然是行不通的。

+2

的可能的复制[Python的:问题与原始\ _Input读取数](http://stackoverflow.com/questions/5762938/python-problem-with-raw-input-reading-a-number) –

+0

“但它不能正常工作”所以出了什么问题?告诉我们你错在哪里,我们可以帮助解释这一方面。 –

+0

可能重复的[我如何在Python中读取输入为整数?](http://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers-in-python) –

回答

2

您需要age_in_days = int(age) * days_in_years使age_in_days是一个数字而不是字符串替换age_in_days = age * days_in_years

+0

谢谢有效。 – charly9011

1

raw_input将返回你通过int导致串重复乘以一个字符串,不是int乘法。

把它包在int呼吁得到你需要

结果
age = int(raw_input("How old are you: ")) 
相关问题