python-3.x - 新手试图在类外的另一个函数中使用从类派生的变量
问题描述
我已经建立了一个类来获取用户输入的日期和时间来创建日期时间对象。我想在另一个函数中使用该对象,但很挣扎。我大约有 2 个月的时间来编码,所以学习曲线陡峭。整个星期都在阅读和观看有关课程的视频,但似乎一直被我自己的代码绊倒
尝试了几次迭代,但怀疑这只是我对此的一些基本缺陷
import datetime
from datetime import date
class User_Input_Date():
"""This class asks the user for a month and a year. It takes the answers and puts them into a date object where day has been assigned to the 1st bc designed to just output month and year."""
def __init__(self):
self.month_input = input('What was the month (1-12)').strip()
if self.month_input in ['01', '1', 'Jan', 'January', 'jan', 'january']:
self.month_input = 1
elif self.month_input in ['02', '2', 'Feb', 'February', 'feb', 'february']:
self.month_input = 2
elif self.month_input in ['03', '3', 'Mar', 'March', 'mar', 'march']:
self.month_input = 3
elif self.month_input in ['04', '4', 'Apr', 'April', 'apr', 'april']:
self.month_input = 4
elif self.month_input in ['05', '5', 'May', 'may']:
self.month_input = 5
elif self.month_input in ['06', '6', 'Jun', 'June', 'jun', 'june']:
self.month_input = 6
elif self.month_input in ['07', '7', 'Jul', 'July', 'jul', 'july']:
self.month_input = 7
elif self.month_input in ['08', '8', 'Aug', 'August', 'aug', 'august']:
self.month_input = 8
elif self.month_input in [
'09', '9', 'Sept', 'September', 'sept', 'september'
]:
self.month_input = 9
elif self.month_input in ['10', 'Oct', 'October', 'oct', 'october']:
self.month_input = 10
elif self.month_input in ['11', 'Nov', 'November', 'nov', 'november']:
self.month_input = 11
elif self.month_input in ['12', 'Dec', 'December', 'dec', 'december']:
self.month_input = 12
else:
self.month_input = None
self.year_input = int(input('What was the year?').strip())
def Combined_User_Input_Date(self):
combine_date_user_input_month_year = datetime.date(
self.year_input, self.month_input, day=1)
return combine_date_user_input_month_year.strftime("%m" + "-" + "%Y")
primary_bariatric_date = User_Input_Date()
print(primary_bariatric_date.Combined_User_Input_Date())
def test_class_output_in_another_function(primary_bariatric_date.Combined_User_Input_Date():
print(str(primary_bariatric_date.Combined_User_Input_Date() + "passed the test")
test_class_output_in_another_function(primary_bariatric_date.Combined_User_Input_Date())
我明白了,
SyntaxError: invalid syntax
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/My Classes/User Input Date Class.py", line 50
def test_class_output_in_another_function(primary_bariatric_date.Combined_User_Input_Date():
^
SyntaxError: invalid syntax
解决方案
我不知道你为什么Combined_User_Input_Date()
一遍又一遍地重复。只调用一次,它是一个函数。我认为您要么打错了,要么一直在思考像 haskell 这样的函数式语言。
import datetime
from datetime import date
class User_Input_Date():
"""This class asks the user for a month and a year. It takes the answers and puts them into a date object where day has been assigned to the 1st bc designed to just output month and year."""
def __init__(self):
self.month_input = input('What was the month (1-12)').strip()
if self.month_input in ['01', '1', 'Jan', 'January', 'jan', 'january']:
self.month_input = 1
elif self.month_input in ['02', '2', 'Feb', 'February', 'feb', 'february']:
self.month_input = 2
elif self.month_input in ['03', '3', 'Mar', 'March', 'mar', 'march']:
self.month_input = 3
elif self.month_input in ['04', '4', 'Apr', 'April', 'apr', 'april']:
self.month_input = 4
elif self.month_input in ['05', '5', 'May', 'may']:
self.month_input = 5
elif self.month_input in ['06', '6', 'Jun', 'June', 'jun', 'june']:
self.month_input = 6
elif self.month_input in ['07', '7', 'Jul', 'July', 'jul', 'july']:
self.month_input = 7
elif self.month_input in ['08', '8', 'Aug', 'August', 'aug', 'august']:
self.month_input = 8
elif self.month_input in [
'09', '9', 'Sept', 'September', 'sept', 'september'
]:
self.month_input = 9
elif self.month_input in ['10', 'Oct', 'October', 'oct', 'october']:
self.month_input = 10
elif self.month_input in ['11', 'Nov', 'November', 'nov', 'november']:
self.month_input = 11
elif self.month_input in ['12', 'Dec', 'December', 'dec', 'december']:
self.month_input = 12
else:
self.month_input = None
self.year_input = int(input('What was the year?').strip())
def Combined_User_Input_Date(self):
combine_date_user_input_month_year = datetime.date(
self.year_input, self.month_input, day=1)
return combine_date_user_input_month_year.strftime("%m" + "-" + "%Y")
primary_bariatric_date = User_Input_Date()
print(primary_bariatric_date.Combined_User_Input_Date())
def test_class_output_in_another_function(primary_bariatric_date):
print(str(primary_bariatric_date.Combined_User_Input_Date()) + "passed the test")
test_class_output_in_another_function(primary_bariatric_date)
推荐阅读
- jmeter - Jmeter Rest API 性能测试 - POST 数据在回放中不起作用
- regex - sed linux命令无法插入行
- c++ - 我在为我的项目使用 C++ STL 优先级队列时遇到了弹出问题
- java - 如何只返回数组中第一次出现的东西
- python - 了解重命名图像 scrapy 的工作原理
- c# - ASP.NET MVC 应用程序以不正确的格式返回日期
- c# - 在 Visio C# 中禁用重叠形状
- android - 将android中状态栏的颜色更改为列表项的颜色
- java - Dropwizard 解压请求过滤器
- python-3.x - 如何在 QTabWidget 上显示多光标?