|
发表于 8-6-2019 12:50 AM
来自手机
|
显示全部楼层
问功课也至少自己动手做,把不会的,遇到问题的地方说明一下吧。
|
|
|
|
|
|
|
|
楼主 |
发表于 13-6-2019 09:31 PM
|
显示全部楼层
这两道问题:
====================
7. Write a function PrintMovieDetails (movie) to print the details of the movie. It takes in a Movie object as its argument and has no return value. This function improves maintainability.
You must use the get functions created to retrieve the movie detailsfor printing.
8. Write a function SearchBasedOnNameOrCategory(searchString, listOfMovies) to search the matching movies from a provided list of movies with the matching searchString. It takes in a searchString of type String and listOfMovie of type List as its arguments and returns result of type List.
This new function replaces the existing search code
第一个file: Movie.py
============
class Movie:
def __init__(self,name,category,description,price):
self.__name=name
self.__category=category
self.__description=description
self.__price=price
def getName(self):
return self.__name
def setName(self,name):
self.__name=name
def getCategory(self):
return self.__category
def setCategory(self,category):
self.__category=category
def getDescription(self):
return self.__description
def setDescription(self,description):
self.description=description
def getPrice(self):
return self.__price
def setPrice(self,price):
self.__price=price
def getPriceWithGST(self):
result=self.__price*1.07
return result
第二个file: MovieApp.py
===============
#我把第一个file import去第二个file
from Movie import Movie
def menu1():
print("\n\nDisplay all movies")
index = 0
usrInput = ""
while usrInput != "M":
print("\nMovie "+str(index+1)+" of "+str(len(listOfMovies)))
print("==============================")
#use new function to display the movie details
print("Name: "+listOfMovies[index].getName())
print("Category: "+listOfMovies[index].getCategory())
print("Description: "+listOfMovies[index].getDescription())
print("Price: $"+str(listOfMovies[index].getPrice()))
print("==============================")
print("Enter N for Next movie")
print("Enter P for Previous movie")
usrInput = input("Enter M to return to Main Menu\n")
# Logic for usrInput
if usrInput == "N":
index += 1
if index >= len(listOfMovies):
index = 0
elif usrInput == "P":
index -= 1
if index < 0:
index = len(listOfMovies)-1
def menu2():
print("\n\nDisplay movie full names for selection")
usrInput = ""
while True:
for index in range(len(listOfMovies)):
print(str(index+1)+". "+listOfMovies[index][0])
print("Enter M to return to Main Menu")
usrInput = input("Please enter your selection\n")
if usrInput == "M":
break
index = int(usrInput)-1
print("\nMovie "+usrInput+" of "+str(len(listOfMovies)))
print("==============================")
#use new function to display the movie movie details
print("Name: "+listOfMovies[index][0])
print("Category: "+listOfMovies[index][1])
print("Description: "+listOfMovies[index][2])
print("Price: $"+str(listOfMovies[index][3]))
print("==============================\n")
innerMenu = ""
innerMenu = input("Enter M to return to Previous Menu. Any key continue.\n")
if innerMenu == "M":
break
def menu3():
print("\n\nSearch based on Name or Category substring")
usrInput = ""
while True:
usrInput = input("Please enter your search input\n")
#use new function to display the movie movie details
filteredList = []
for index in range(len(listOfMovies)):
if usrInput.lower() in listOfMovies[index][0].lower() or usrInput.lower() in listOfMovies[index][1].lower():
filteredList.append(listOfMovies[index])
if len(filteredList) == 0:
print("\n======= No results found =======")
else:
for index in range(len(filteredList)):
print("\nMovie "+str(index+1)+" of "+str(len(filteredList)))
#use new function to display the movie details
print("==============================")
print("Name: "+filteredList[index][0])
print("Category: "+filteredList[index][1])
print("Description: "+filteredList[index][2])
print("Price: $"+str(filteredList[index][3]))
print("==============================")
usrInput = "reset"
while usrInput != "1" and usrInput != "2" :
print("1. Search again")
usrInput = input("2. Return to Main Menu\n")
if usrInput == "2":
break
listOfMovies=[]
file=open('input.txt')
lines=file.readlines()
for data in lines:
data=data.replace("\n","")
cols=data.split("|")
name=cols[0]
category=cols[1]
description=cols[2]
price=cols[3]
m=Movie(name,category,description,price)
listOfMovies.append(m)
file.close()
while True:
print("Main Menu")
print("----------")
print("1. Display all movies")
print("2. Display movie full names for selection")
print("3. Search based on Name or Category substring")
print("Q. Enter Q to quit")
menuSelection = input("Please input your selection\n")
if menuSelection == "Q":
break
print("You have selected "+menuSelection+".. ",end="")
if menuSelection == "1":
#include exception handling for index error
menu1()
elif menuSelection == "2":
#include exception handling for index error
menu2()
elif menuSelection == "3":
menu3()
|
|
|
|
|
|
|
|
发表于 13-6-2019 10:01 PM
|
显示全部楼层
本帖最后由 flashang 于 13-6-2019 10:15 PM 编辑
gnow10 发表于 13-6-2019 09:31 PM
这两道问题:
====================
7. Write a function PrintMovieDetails (movie) to print the details of the movie. It takes in a Movie object as its argument and has no return value. This functi ...
好的。现在遇到什么问题了?
有没有 error message ? line number ?
|
|
|
|
|
|
|
|
楼主 |
发表于 13-6-2019 10:52 PM
|
显示全部楼层
总共有3个menu,我要怎么把第8题的问题取代我现在的menu 3?就是说我不要用我现在的menu 3来Search based on Name or Category substring 。我要create 一个function SearchBasedOnNameOrCategory(searchString,listOfMovies) 来search Name 或者category
我已经create了一个 在上面
def SearchBasedOnNameOrCategory(searchString, listOfMovies):
ofTypeList=[]
然后问号那边要怎么编?
from Movie import Movie
def SearchBasedOnNameOrCategory(searchString, listOfMovies):
ofTypeList=[]
????????
return ofTypeList
def menu1():
print("\n\nDisplay all movies")
index = 0
usrInput = ""
while usrInput != "M":
print("\nMovie "+str(index+1)+" of "+str(len(listOfMovies)))
print("==============================")
#use new function to display the movie details
print("Name: "+listOfMovies[index].getName())
print("Category: "+listOfMovies[index].getCategory())
print("Description: "+listOfMovies[index].getDescription())
print("Price: $"+str(listOfMovies[index].getPrice()))
print("==============================")
print("Enter N for Next movie")
print("Enter P for Previous movie")
usrInput = input("Enter M to return to Main Menu\n")
# Logic for usrInput
if usrInput == "N":
index += 1
if index >= len(listOfMovies):
index = 0
elif usrInput == "P":
index -= 1
if index < 0:
index = len(listOfMovies)-1
def menu2():
print("\n\nDisplay movie full names for selection")
usrInput = ""
while True:
for index in range(len(listOfMovies)):
print(str(index+1)+". "+listOfMovies[index].getName())
print("Enter M to return to Main Menu")
usrInput = input("Please enter your selection\n")
if usrInput == "M":
break
index = int(usrInput)-1
print("\nMovie "+usrInput+" of "+str(len(listOfMovies)))
print("==============================")
#use new function to display the movie movie details
print("Name: "+listOfMovies[index].getName())
print("Category: "+listOfMovies[index].getCategory())
print("Description: "+listOfMovies[index].getDescription())
print("Price: $"+str(listOfMovies[index].getPrice()))
print("==============================\n")
innerMenu = ""
innerMenu = input("Enter M to return to Previous Menu. Any key continue.\n")
if innerMenu == "M":
break
def menu3():
print("\n\nSearch based on Name or Category substring")
usrInput = ""
while True:
usrInput = input("Please enter your search input\n")
#use new function to display the movie movie details
filteredList = []
for index in range(len(listOfMovies)):
if usrInput.lower() in listOfMovies[index].getName().lower() or usrInput.lower() in listOfMovies[index].getCategory().lower():
filteredList.append(listOfMovies[index])
if len(filteredList) == 0:
print("\n======= No results found =======")
else:
for index in range(len(filteredList)):
print("\nMovie "+str(index+1)+" of "+str(len(filteredList)))
#use new function to display the movie details
print("==============================")
print("Name: "+filteredList[index].getName())
print("Category: "+filteredList[index].getCategory())
print("Description: "+filteredList[index].getDescription())
print("Price: $"+str(filteredList[index].getPrice()))
print("==============================")
usrInput = "reset"
while usrInput != "1" and usrInput != "2" :
print("1. Search again")
usrInput = input("2. Return to Main Menu\n")
if usrInput == "2":
break
listOfMovies=[]
file=open('input.txt')
lines=file.readlines()
for data in lines:
data=data.replace("\n","")
cols=data.split("|")
name=cols[0]
category=cols[1]
description=cols[2]
price=cols[3]
m=Movie(name,category,description,price)
listOfMovies.append(m)
'''
print("The list of movies are:")
for m in listOfMovies:
print(m.getTitle())
'''
file.close()
while True:
print("Main Menu")
print("----------")
print("1. Display all movies")
print("2. Display movie full names for selection")
print("3. Search based on Name or Category substring")
print("Q. Enter Q to quit")
menuSelection = input("Please input your selection\n")
if menuSelection == "Q":
break
print("You have selected "+menuSelection+".. ",end="")
if menuSelection == "1":
#create function for running menu selection 1 logic
#include exception handling for index error
menu1()
elif menuSelection == "2":
#create function for running menu selection 2 logic
#include exception handling for index error
menu2()
elif menuSelection == "3":
#create function for running menu selection 3 logic
menu3()
|
|
|
|
|
|
|
|
发表于 13-6-2019 11:57 PM
|
显示全部楼层
本帖最后由 flashang 于 14-6-2019 12:20 AM 编辑
传入的有
searchString, listOfMovies
也就是说在 listOfMovies 里面找到所有 searchString 的,然后
把找到的记录放入 ofTypeList
ofTypeList.append(listOfMovies[index])
如果需要修改资料,可以用回你写的方法
m = Movie(name, category, description, price)
ofTypeList.append(m)
p/s : 第七题的 PrintMovieDetails 没做?
|
|
|
|
|
|
|
|
发表于 14-6-2019 12:15 AM
|
显示全部楼层
gnow10 发表于 13-6-2019 10:52 PM
总共有3个menu,我要怎么把第8题的问题取代我现在的menu 3?就是说我不要用我现在的menu 3来Search based on Name or Category substring 。我要create 一个function SearchBasedOnNameOrCategory(sea ...
之前的 2,3 中的问题也解决了。
不过,选 2 输入 > 5 或者其他英文字母会有 error.
|
|
|
|
|
|
|
|
楼主 |
发表于 14-6-2019 01:15 AM
|
显示全部楼层
flashang 发表于 13-6-2019 11:57 PM
传入的有
searchString, listOfMovies
也就是说在 listOfMovies 里面找到所有 searchString 的,然后
把找到的记录放入 ofTypeList
ofTypeList.append(listOfMovies)
如果需要修改资料,可以用回你写的 ...
我需要return ofTypeList,你看我写的对吗?
def menuSearchBasedOnNameOrCategory(searchString,listOfMovies):
print("\n\nSearch based on Name or Category substring")
searchString = ""
while True:
searchString = input("Please enter your search input\n")
#use new function to display the movie movie details
ofTypeList = []
for index in range(len(listOfMovies)):
if searchString.lower() in listOfMovies[index].getName().lower() or searchString.lower() in listOfMovies[index].getCategory().lower()==searchString:
ofTypeList.append(listOfMovies[index])
return ofTypeList
但是我要怎么call和运用这个function呢?有error,怎么解决这个error?
是不是我写错了?searchString那边系统说没define到
elif menuSelection == "3":
ofTypeList=menuSearchBasedOnNameOrCategory(searchString,listOfMovies)
print(ofTypeList)
第7题我不知道要怎么define function PrintMovieDetails(movie) ,先解决这第8题再请教你第7题
|
|
|
|
|
|
|
|
发表于 14-6-2019 08:12 AM
来自手机
|
显示全部楼层
本帖最后由 flashang 于 14-6-2019 08:39 AM 编辑
gnow10 发表于 14-6-2019 01:15 AM
我需要return ofTypeList,你看我写的对吗?
def menuSearchBasedOnNameOrCategory(searchString,listOfMovies):
print("\n\nSearch based on Name or Category substring"
searchString = ""
...
参考 menu2 的做法,
做一个 menu3b 来接受输入,并且用 searchString 来呼叫menuSearchBasedOnNameOrCategory。
之后就参考 menu1 的做法,把结果显示出来。
用 range 来显示也可以。
|
|
|
|
|
|
|
|
楼主 |
发表于 14-6-2019 09:17 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 14-6-2019 09:54 PM
来自手机
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 14-6-2019 10:17 PM
|
显示全部楼层
flashang 发表于 14-6-2019 09:54 PM
elif menuSelection == "3":
menu3b()
在 menu3b() 可以用回之前做的
searchString = ""
while True:
searchString = input("lease enter your search input\n")
然 ...
elif menuSelection == "3":上面这个是要呼叫的 menuSearchBasedOnNameOrCategory(searchString,listOfMovies)不是吗?怎么用来呼叫menu3b()了呢?
你说的menu3b()是要我新建的一个function对吗?
|
|
|
|
|
|
|
|
发表于 14-6-2019 10:51 PM
来自手机
|
显示全部楼层
gnow10 发表于 14-6-2019 10:17 PM
elif menuSelection == "3":上面这个是要呼叫的 menuSearchBasedOnNameOrCategory(searchString,listOfMovies)不是吗?怎么用来呼叫menu3b()了呢?
你说的menu3b()是要我新建的一个function对吗 ...
是的。因为
menuSearchBasedOnNameOrCategory(searchString,listOfMovies)
需要由被呼叫。
|
|
|
|
|
|
|
|
楼主 |
发表于 14-6-2019 11:15 PM
|
显示全部楼层
出来的结果为什么是这样的?当系统让我enter search input 的时候,我enter了 action 然后就出现下面这个结果了
Please input your selection
3
You have selected 3..
Please enter your search input
action
Search based on Name or Category substring
[<Movie.Movie object at 0x0000017986749BA8>, <Movie.Movie object at 0x0000017986749D30>]
Please enter your search input
code错了吗?
def menu3b():
searchString = ""
while True:
searchString = input("\nPlease enter your search input\n")
ofTypeList=menuSearchBasedOnNameOrCategory(searchString,listOfMovies)
print(ofTypeList)
def menuSearchBasedOnNameOrCategory(searchString,listOfMovies):
print("\n\nSearch based on Name or Category substring")
#use new function to display the movie movie details
ofTypeList = []
for index in range(len(listOfMovies)):
if searchString.lower() in listOfMovies[index].getName().lower() or searchString.lower() in listOfMovies[index].getCategory().lower()==searchString:
ofTypeList.append(listOfMovies[index])
return ofTypeList
if len(ofTypeList) == 0:
print("\n======= No results found =======")
else:
for index in range(len(ofTypeList)):
print("\nMovie "+str(index+1)+" of "+str(len(ofTypeList)))
#use new function to display the movie details
print("==============================")
print("Name: "+ofTypeList[index].getName())
print("Category: "+ofTypeList[index].getCategory())
print("Description: "+ofTypeList[index].getDescription())
print("Price: $"+str(ofTypeList[index].getPrice()))
print("==============================")
usrInput = "reset"
while usrInput != "1" and usrInput != "2" :
print("1. Search again")
usrInput = input("2. Return to Main Menu\n")
if usrInput == "2":
break
listOfMovies=[]
file=open('input.txt')
lines=file.readlines()
for data in lines:
data=data.replace("\n","")
cols=data.split("|")
name=cols[0]
category=cols[1]
description=cols[2]
price=cols[3]
m=Movie(name,category,description,price)
listOfMovies.append(m)
'''
print("The list of movies are:")
for m in listOfMovies:
print(m.getTitle())
'''
file.close()
while True:
print("Main Menu")
print("----------")
print("1. Display all movies")
print("2. Display movie full names for selection")
print("3. Search based on Name or Category substring")
print("Q. Enter Q to quit")
menuSelection = input("Please input your selection\n")
if menuSelection == "Q":
break
print("You have selected "+menuSelection+".. ",end="")
if menuSelection == "1":
#include exception handling for index error
menu1()
elif menuSelection == "2":
#include exception handling for index error
menu2()
elif menuSelection == "3":
menu3b()
应该要出现下面这样才对呀
Movie 1 of 2
==============================
Name: Mission: Impossible - Fallout
Category: Action
Description: Ethan Hunt and his IMF team, along with some familiar allies, race against time after a mission gone wrong.
Price: $20
==============================
Movie 2 of 2
==============================
Name: Incredibles 2
Category: Action
Description: Bob Parr (Mr. Incredible) is left to care for the kids while Helen (Elastigirl) is out saving the world.
Price: $12
==============================
1. Search again
2. Return to Main Menu
|
|
|
|
|
|
|
|
发表于 15-6-2019 08:34 AM
来自手机
|
显示全部楼层
本帖最后由 flashang 于 15-6-2019 08:37 AM 编辑
gnow10 发表于 14-6-2019 11:15 PM
出来的结果为什么是这样的?当系统让我enter search input 的时候,我enter了 action 然后就出现下面这个结果了
Please input your selection
3
You have selected 3..
Please enter your search input
actio ...
显示结果是由 print(ofTypeList) 执行。
而 ofTypeList 是从 listOfMovies 选出来的,这两个 variable 是同样的 type。
你是怎么显示 listOfMovies 的内容呢?
|
|
|
|
|
|
|
|
楼主 |
发表于 15-6-2019 10:36 AM
|
显示全部楼层
你说的显示 listOfMovie的内容是指这个吗?
listOfMovies=[]
file=open('input.txt')
lines=file.readlines()
for data in lines:
data=data.replace("\n","")
cols=data.split("|")
name=cols[0]
category=cols[1]
description=cols[2]
price=cols[3]
m=Movie(name,category,description,price)
listOfMovies.append(m)
|
|
|
|
|
|
|
|
发表于 15-6-2019 11:38 AM
|
显示全部楼层
本帖最后由 flashang 于 15-6-2019 11:43 AM 编辑
试试看解释这几行
- file=open('input.txt') # 打开档案 input.txt
- lines=file.readlines() # 读取档案
- for data in lines: # 每一列处理:
- data=data.replace("\n","") # 把 换行 符号去除
- cols=data.split("|") # 用 | 符号分隔行
- name=cols[0] # 取得 name
- category=cols[1] # 取得 category
- description=cols[2] # 取得 description
- price=cols[3] # 取得 price
- m=Movie(name,category,description,price) # 用取得的资料建立 数据 m,类别为 Movie
- listOfMovies.append(m) # 在 m 加在 listOfMovies 的最后面
复制代码
这里没有 “显示” 的动作。
怎么样吧 listOfMovies 的内容正确的 print 出来呢?
|
|
|
|
|
|
|
|
楼主 |
发表于 15-6-2019 09:33 PM
|
显示全部楼层
不行呀大大,我如果在这里放 “显示” 的动作的话,listOfMovies 的内容不是的 print 在main menu吗?
你看看我是不是编错码了
|
|
|
|
|
|
|
|
发表于 15-6-2019 10:04 PM
来自手机
|
显示全部楼层
本帖最后由 flashang 于 15-6-2019 10:05 PM 编辑
gnow10 发表于 15-6-2019 09:33 PM
不行呀大大,我如果在这里放 “显示” 的动作的话,listOfMovies 的内容不是的 print 在main menu吗?
你看看我是不是编错码了
。。。
把 print(ofTypeList) 改了。
用 print listOfMovies 的方式来 print ofTypeList.
|
|
|
|
|
|
|
|
楼主 |
发表于 15-6-2019 11:03 PM
|
显示全部楼层
谢谢大大。第8题已经解决了。想再请教你第7题:
7. Write a function PrintMovieDetails(movie) to print the details of the movie. It takes in a Movie object as its argument and has noreturn value.
我已经建多一个function PrintMovieDetails (movie) 在上面了,应该要怎么更改现有的code呢?
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|