【问题标题】:TypeError: unsupported operand type(s) for -: 'int' and 'str' with a datetime moduleTypeError: 不支持的操作数类型 -: 'int' and 'str' with a datetime module
【发布时间】:2018-05-27 18:20:13
【问题描述】:

我想使用 datetime 模块和用户输入来制作一个简单的年龄计算器。这是我的代码:

from datetime import datetime
now = datetime.now()
    if day == now.day and month == now.month:
         age = now.year - year
    if day != now.day or  month != now.month:
         age  = now.year - age - 1
    if now.year - year  == -1:
        print("Be serious, please.")
    if now.year - rok < -1:
        print("Be serious,please.")
    print(age)

它给了我以下错误:age = now.year - rok - 1 TypeError: 不支持的操作数类型 -: 'int' 和 'str' 当我用我分配给它们的变量替换 now.day 和 now.month 和 now.year 时,它也会给我这个错误,唯一的区别是,它显示 current_year 而不是 now.year。

from datetime import datetime
now = datetime.now()
today = now.day 
current_month = now.month 
current_year = now.year
if day == now.day and month == now.month:
          age = current_year - year
if day != now.day or  month != now.month:
    age  = current_year - age - 1
if current_year - year  == -1:
    print("Be serious, please.")
if now.year - rok < -1:
    print("Be serious,please.")
print(age)

【问题讨论】:

  • 您没有在提供的代码中定义 day,month,yearrok 并且 age 定义不明确。确保这些项目 a) 正确定义和 b) 它们的类型为 int
  • 您所需要的只是不要将数据类型混合在一起。不能比较,例如整数 5 和字符串“5”,因为它等于失败。 rok 是什么类型的数据?这是整数类型吗?我无法在任何地方的代码中看到它的定义。
  • 哦。 Rok 应该是年,这个错误发生在我将变量名称翻译成英文时。

标签: python python-3.x


【解决方案1】:

这里有一些伪代码可以帮助您解决问题:

import datetime
polling = True
while polling:
   print("Please enter your birthday with this format(year, month, day)")
   user_year = int(input('Which year were you born in?: '))
   user_month = int(input('Which month were you born in?: '))
   user_day = int(input("which day of the month were you born on?: "))
   birthday = datetime.datetime(user_year, user_month, user_day)
   now = datetime.datetime.now()

   if user_day == now.day and month == now.month:
      age = now.year - birthday.year

   elif user_day != now.day or user_month != now.month and now.hour - birthday.hour > 24:
      age = (now.year - birthday.year) - 1

   elif user_day != now.day or user_month != now.month and now.hour - birthday.hour < 24:
    age = now.year - birthday.year

   if age <= 0:
     print('Be serious man')
     print('Enter your real age')
     continue

   else:
     print('You age is: {}'.format(age))

     prompt = input("Would you like to allow someone else to enter their age?(yes/no): ")
     if prompt.lower == 'no':
        polling = False

这是一个输出示例:

Please enter your birthday with this format(year, month, day)
Which year were you born in?: 1992
Which month were you born in?: 05
which day of the month were you born on?: 26
You age is: 25
Would you like to allow someone else to enter their age?(yes/no): yes
Please enter your birthday with this format(year, month, day)
Which year were you born in?: 2019
Which month were you born in?: 04
which day of the month were you born on?: 20
Be serious man
Enter your real age
Please enter your birthday with this format(year, month, day)
Which year were you born in?: 

这只是一些伪代码,但问题的根源可能是您没有确保用户输入是整数格式。这就是为什么我们在这段代码中输入之前传递了 int。为了使 datetime.datetime 与日期一起工作,您必须将其设为整数输入,除非您使用 strptime() 来解析字符串输入,我也可以向您展示如何操作。接下来,如果只有一个条件可以为 True,最好在代码中使用 elif 语句而不是 if 语句。有人不可能在同一天出生并在另一天出生,所以 elif 在这里比 if 更有效。我们还需要添加一个循环,因为有人可以输入一个

【讨论】:

    猜你喜欢
    • 2016-05-11
    • 2016-04-15
    • 2012-11-29
    • 2012-12-31
    • 1970-01-01
    • 2022-06-11
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多