接收一个字符串,转换为计算并显示响应,但是..不能拆分 [英] Receive a string, convert to calculate and display response, but.. Can't split

查看:26
本文介绍了接收一个字符串,转换为计算并显示响应,但是..不能拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的作业问题:

创建一个脚本来接收出生日期并返回用户的年龄

Create a script to receive the birth date and return the user's age

我试过了:

ano_atual = 2012                                       
data_nascimento = input('Digite sua data de nascimento (dd/mm/aaaa): ')
dia, mes, ano = map(int, data_nascimento.split('/'))
idade = ano_atual - ano
print idade

但我被这个错误困住了:

But I am stuck with this error:

AttributeError: 'int' object has no attribute 'split'

推荐答案

你可能想要 raw_input 而不是 input.input 相当于python2.x 中的eval(raw_input()).所以你的字符串被转换为整数.就像:

You probably want raw_input instead of input. input is equivalent to eval(raw_input()) in python2.x. So your string is being cast to an integer. just like:

a=30/6/2012

会.(在本例中,a 是 30 除以 6 除以 2012 的结果,因为 Python 2 中的整数除法而为 0.)

would be. (In this case, a is the result of 30 divided by 6 divided by 2012 which is 0 because of integer division in python 2.)

这篇关于接收一个字符串,转换为计算并显示响应,但是..不能拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆