这是我的第一个问题
我正在自学如何使用Python(随后是django)编写代码。我正在开发一个网站,让当地的帆船赛能够创建团队并跟踪他们的成绩。虽然这最终将是一个使用数据库的django项目,但我想编写一个简单的脚本来“勾勒”逻辑
目标:我希望用户能够创建一个比赛组,将船只添加到此组,并打印各种项目
当前代码:我编写了基本脚本,允许用户将船只添加到现有比赛组:
#basic program logic to add boats to an existing race group;
#existing race group:
shediac = {
'location':'Shediac NB',
'year':2020,
'boats': boats
}
#default boat list to pass into the race group
boats=[
{'name':'name1','owner':'owner1','handicap':0.00},
]
#loop to take user input when adding new entries
answer=input('do you want to add a boat?: Y/N').upper()
while answer == 'Y':
name = input('enter the boat name: ')
owner = input('enter the boat owner''s name: ')
handicap = input('enter the boat handicap: ')
boats.append({
'name': name,
'handicap': handicap,
'owner': owner,
})
# get user input again to retest for the while loop
answer=input('do you want to add a boat?: Y/N').upper()
#prompt user to select information to display:
while true:
what = input('what do you want to view: NAMES / OWNERS / HANDICAP / EXIT:
').lower()
if what == 'names':
for boat in shediac['boats']:
print(boat['name'])
elif what == 'owners':
for boat in shediac['boats']:
print(boat['owner'])
elif what == 'handicap':
for boat in shediac['boats']:
print(boat['handicap'])
else:
print('see you next time')
挑战:
如何让用户创建新的种族组
如何利用用户输入生成新种族组的名称
我为每个比赛组使用一个字典,并传递一个船只列表(包含各种键值对的字典)。现有代码用于将船条目添加到现有比赛组(字典)
如果我的方法完全错误,我欢迎任何更好的解决方案!我的主要兴趣是了解如何处理这样的问题
谢谢
虽然将内容存储在字典中是可以的,但有时使用专用类型更为清晰:
接下来,定义一些输入方法。下面是一个返回
Boat
的方法:现在来看一个返回
Boat
列表的方法。我们可以在循环中重用input_boat
:下面是一个返回
RaceGroup
的方法:当你把事情分解成子问题时,编程更容易,代码也更清晰
我们现在可以使用上面在主程序中创建的函数“库”:
相关问题 更多 >
编程相关推荐