佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

12
返回列表 发新帖
楼主: gnow10

python

[复制链接]
发表于 15-6-2019 11:55 PM 来自手机 | 显示全部楼层
gnow10 发表于 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 已经做好了。
所以,需要在 main menu 呼叫这个 function 吧?



回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 16-6-2019 01:16 AM | 显示全部楼层
flashang 发表于 15-6-2019 11:55 PM
根据问题,写一个 function 已经做好了。
所以,需要在 main menu 呼叫这个 function 吧?

不是在main menu,是在menu1里面,需要在有关print movie details的部分一律call这个新function,要怎么更改现有的code?


def PrintMovieDetails(movie):   
    print("Name: "+movie.getName())
    print("Category: "+movie.getCategory())
    print("Description: "+movie.getDescription())
    print("Price: $"+str(movie.getPrice()))
   


def menu1():   
    print("\n\nDisplay all movies")
    index = 0
    usrInput = ""
    while usrInput != "M":
        try:      
            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


        except Exception as e:
            print("IndexError occurred")



回复

使用道具 举报

发表于 16-6-2019 01:36 AM 来自手机 | 显示全部楼层
gnow10 发表于 16-6-2019 01:16 AM
不是在main menu,是在menu1里面,需要在有关print movie details的部分一律call这个新function,要怎么更改现有的code?


def PrintMovieDetails(movie):   
    print("Name: "+movie.getName())
    prin ...

首先,要知道改哪里?

既然做好了
PrintMovieDetails(movie)
当然是要取代 “显示” 的部分。
所以,找到这个地方,用新的 function 处理显示的动作。



回复

使用道具 举报

 楼主| 发表于 16-6-2019 12:07 PM | 显示全部楼层
flashang 发表于 16-6-2019 01:36 AM
首先,要知道改哪里?

既然做好了
PrintMovieDetails(movie)
当然是要取代 “显示” 的部分。
所以,找到这个地方,用新的 function 处理显示的动作。

我知道新的function是要取代 “显示” 的部分但是我不知道要怎么更改现有的code来呼叫这个新的function,你可以教我要怎么在这个menu1里面call这个新function吗?然后我就可以按照你教我的在menu2和menu3里面call这个新function了。

青色部分是要用PrintMovieDetails(movie)的function来显示的

def PrintMovieDetails(movie):   
    print("Name: "+movie.getName())
    print("Category: "+movie.getCategory())
    print("Description: "+movie.getDescription())
    print("Price: $"+str(movie.getPrice()))
   
def menu1():   
    print("\n\nDisplay all movies")
    index = 0
    usrInput = ""
    while usrInput != "M":
        try:      
            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

        except Exception as e:
            print("IndexError occurred")
回复

使用道具 举报

发表于 16-6-2019 12:16 PM 来自手机 | 显示全部楼层
本帖最后由 flashang 于 16-6-2019 12:20 PM 编辑
gnow10 发表于 16-6-2019 12:07 PM
我知道新的function是要取代 “显示” 的部分但是我不知道要怎么更改现有的code来呼叫这个新的function,你可以教我要怎么在这个menu1里面call这个新function吗?然后我就可以按照你教我的在menu2和menu3里面call ...


用推理来分析如何呼叫 function.

PrintMovieDetails(movie)

而显示 找到的其中一个 Movie 的部分内容是
            print("rice: $"+str(listOfMovies[index].getPrice()))

试试看拼凑一下再稍微改一点?




回复

使用道具 举报

 楼主| 发表于 16-6-2019 03:42 PM | 显示全部楼层
flashang 发表于 16-6-2019 12:16 PM
用推理来分析如何呼叫 function.

PrintMovieDetails(movie)

而显示 找到的其中一个 Movie 的部分内容是
            print("Price: $"+str(listOfMovies.getPrice()))

试试看拼凑一下再稍微改一点?
...

毫无头绪。。一头雾水。。
回复

使用道具 举报

Follow Us
发表于 16-6-2019 03:54 PM 来自手机 | 显示全部楼层
gnow10 发表于 16-6-2019 03:42 PM
毫无头绪。。一头雾水。。

参考第八题,如何 call function with parameter ?



回复

使用道具 举报

 楼主| 发表于 16-6-2019 04:30 PM | 显示全部楼层
flashang 发表于 16-6-2019 03:54 PM
参考第八题,如何 call function with parameter ?

想得到的话我就不需要问了。Anyway, 谢谢你
回复

使用道具 举报


ADVERTISEMENT

发表于 16-6-2019 04:41 PM 来自手机 | 显示全部楼层
本帖最后由 flashang 于 16-6-2019 04:46 PM 编辑
gnow10 发表于 16-6-2019 04:30 PM
想得到的话我就不需要问了。Anyway, 谢谢你


呼叫 PrintMovieDetails(movie)

试试看这个?用 PrintMovieDetails(listOfMovies[index])

传入的 parameter 是 Movie type.



回复

使用道具 举报

 楼主| 发表于 16-6-2019 05:38 PM | 显示全部楼层
flashang 发表于 16-6-2019 04:41 PM
呼叫 PrintMovieDetails(movie)

试试看这个?用 PrintMovieDetails(listOfMovies)

传入的 parameter 是 Movie type.

可以了大大 谢谢你

想再请教你一个问题,我先前已经建立一个Movie class,如果我想在我的code里面getPriceWithGST() 该怎么做?

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



如果我想在我的code里面getPriceWithGST() 该怎么做?

Question:

Note:getPriceWithGST()
Example:
                            i.            E.g. Price of movieis $10, price with GST would be $10 x 1.07 = $10.7
                           ii.             Call this function in your code to display price with GST.



回复

使用道具 举报

发表于 16-6-2019 06:56 PM 来自手机 | 显示全部楼层
gnow10 发表于 16-6-2019 05:38 PM
可以了大大 谢谢你

想再请教你一个问题,我先前已经建立一个Movie class,如果我想在我的code里面getPriceWithGST() 该怎么做?

class Movie:

    def __init__(self,name,category,descripti ...

既然是显示价格,就在所有 显示价格的地方 使用这个 function.



回复

使用道具 举报

 楼主| 发表于 17-6-2019 03:29 PM | 显示全部楼层
flashang 发表于 16-6-2019 06:56 PM
既然是显示价格,就在所有 显示价格的地方 使用这个 function.

好的 谢谢你
回复

使用道具 举报

 楼主| 发表于 3-8-2019 04:00 PM | 显示全部楼层
大大,请教你一个问题。我要删除database里面的文件,如果删除的时候无意输入database里不存在的文件就会prompt出一个error说"No such data in database",我应该要怎么code呢?如果我用try except的话又要code在哪里呢?

def deleteMovie():

    name=txtName.get()

    if name=="":
        messagebox.showinfo("Delete","Please enter <Name> to delete!")
    else:
        conn = sqlite3.connect('myMovieApp.db')
        sql="Delete from movie where name=?"
        conn.execute(sql,(name,))
        conn.commit()
        messagebox.showinfo("Success","<"+name+"> Delete Successful")

回复

使用道具 举报

发表于 13-8-2019 02:16 PM 来自手机 | 显示全部楼层
gnow10 发表于 3-8-2019 04:00 PM
大大,请教你一个问题。我要删除database里面的文件,如果删除的时候无意输入database里不存在的文件就会prompt出一个error说"No such data in database",我应该要怎么code呢?如果我用try except的话又要code在哪 ...

没有 “回复”  或者 @ user name 是不会收到通知的。

看到使用 sql, 所以推测是另外一个程式,或者是把资料处理部分改成 sql 了。

可以用 select... 来检查有多少个记录符合条件,
然后才做 delete.



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 29-3-2024 01:46 AM , Processed in 0.067393 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表