笨办法学Python3习题45(我自己做的一个选择脚本)

花了三天时间。(以下是一些废话!!!)

第一天下午上课的时候在构思地图,晚上就开始写,刚开始大部分还是照着43的脚本去写,感觉挺不错,后面也修修改改了蛮多,写成最后自己想要的样子。

第二天上课的时候就是在想一些其他地方的语句应该写什么,当然了很多地方都还需要去翻译成英文的,下午写得挺累的,晚上果断打了会游戏,然后去羽毛球场打了一个半小时球,期间还和几个研二研三的师兄聊了一下我打算转码的成功率之类的话题,打完羽毛球他们走了,我就接着去田径场慢跑了2km。洗完澡之后写代码效率果然大涨,脑子不再一团浆糊,很多之前没写好的函数和一些赋值调用问题,居然轻轻松松就解决掉了,而且还只花了一点点时间。一点多的时候以为已经写好了,结果分成几个文件的时候还是漏洞百出,运行不了。之后反复试了几下没成功,就带着问题去睡觉了。

第三天,早上打游戏下午打游戏,期间就偶尔想想昨天的问题,当然了打游戏的时间多很多,就是偶尔想一想改一改,晚上看电影,看完去跑步洗澡打了把游戏就12点了才开始写,还别说灵感挺好的,问题都解决了,还修改了一些之前没有发现的错误。写到2点钟的时候终于彻底搞完了,自己也调试了很多次,当然也还是会出一些错误的,只是我能力有限可能暂时没有发现,如果你发现了可以评论区和我说一下。(ps:ex45x是我的文件名)

                                                               这是总文件的代码             

from sys import exit
from random import randint

class DoWhat(object):# (object)==(MyChoice(选项))

    def __init__(self, do_this):
        self.do_this = do_this#这两行代码大概意思就是将self.do_this去使用以 do_this为名的这个类名了,可见11行就是应用。
#        print(self.do_this)  以下有这种样式的#字符,都是为了方便理解 设置的打印,可以在终端运行的时候,看看到底发生了什么。

    def do(self):
        want_to_do_now = self.do_this.first_thing()#return的路径:240→243→10
#        print(want_to_do_now)
#   这是刚开始写的时候的错误代码,没舍得删除     really_want_to_do = self.do_this.consider_again()

        while want_to_do_now != 1:
            next_want_to_do = want_to_do_now.going_to_do()#运行 某个类里面的 going_to_do函数
            true_answer =want_to_do_now.doing()#运行某个类里面的 doing()函数,返还一个字符串,进行if语句
#            a =want_to_do_now
#            print(a,b,next_want_to_do)

            if true_answer ==next_want_to_do: #这种不是赋值的需要用两个符号啊。
                new_sentence =[
                f"\nThen you have to get seriously about {true_answer}.",
                f"\nRemember {true_answer} seriously.",
                f"\nYou don't have to think about others when you are {true_answer}.",
                f"\nWhen you finish the thing of {true_answer}, welcome you come here again!"
                ]

                print(new_sentence[randint(0,3)])#随机数表,[0,3]刚开始做了个[0,4]以为取左不取右,记混了,结果 出现了list index out of range这个问题
                exit(1)
            else:
                print("I believe your new thought is better than the other")
            want_to_do_now = self.do_this.consider_again(next_want_to_do)


class WatchMovie():

    def doing(self):
        return 'watching movie'

    def going_to_do(self):
        print("""
            Welcome come here to think about weather you should watch
            movie.If you have movie which you want to watch and you are
            avalible and long time no watch and you have learnt many days.
            You can watch,else you need to consider again.If you will
            watch movie please input #1 ,else #2"""
            )

        your_answer1 = input(">")

        if your_answer1 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya1 = input(">>>>watching movie:")#ya 就是your_answer的缩写,1代表的这个是第一个调用
            return ya1

        elif your_answer1 =="2":
            next_Step1 = input("Do other what? Please input your new thought!\n>")
            next_step1 = Choices.get(next_Step1)
            return next_step1

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'


class PlayComputerGame():

    def doing(self):
        return 'playing computer'

    def going_to_do(self):
        print("""
            Welcome come here to think about weather you should play
            computer game .First, if you haven't study emotion and you
            specially want to play some hero.Second ,your junior high
            school classmates(at least two) is waiting someone or your
            senior high school classmates invite you to join their team!
            If one of these conditions is met,please input #1.Else,#2."""
            )

        your_answer2 = input(">")

        if your_answer2 == "1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya2 =input(">>>>PlayComputer:")
            return ya2

        elif your_answer2 =="2":
            next_Step2 = input("Do other what? Please input your new thought!\n>")
            next_step2 = Choices.get(next_Step2)
            return next_step2

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'


class ListenMusic():

    def doing(self):
        return 'listening music'

    def going_to_do(self):
        print("""
            Welcome come here to think about weather you should listen
            music .If you heard it two hours ago or you have new songs
            to listen to.It's a good choice that you go to listen to
            music next.\tIf you really decide to listen to music,input
            #1 ,else:2"""
            )

        your_answer3 =input(">")

        if your_answer3 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya3 =input(">>>>ListenMusic:")
            return ya3

        elif your_answer3 =="2":
            next_Step3 = input("Do other what? Please input your new thought!\n>")
            next_step3 = Choices.get(next_Step3)
            return next_step3

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'


class GoOutside():

    def doing(self):
        return 'going outside'

    def going_to_do(self):
        print("""
            Welcome come here to think about weather you should go
            outside.If you are very free and specially going to go outside
            as well as you haven't had a 50¥ daily bill for almost a week.
            You should go outside to enjoy the free time immediately.So ,if
            intend to going outside please input #1,else 2."""
            )

        your_answer4 =input(">")

        if your_answer4 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya4 =input(">>>>GoOutside:")
            return ya4

        elif your_answer4 =="2":
            next_Step4 = input("Do other what? Please input your new thought!\n>")
            next_step4 = Choices.get(next_Step4)
            return next_step4

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'


class EatRice():

    def doing(self):
        return 'eating rice'

    def going_to_do(self):
        print("""
            Welcome come here to think about whether you should eat rice.
            First, if it's almost dinner time.Second, you have been hungry
            for a long time. Third, you go to the dining hall is right at
            present! If you meet all the conditions please input #1 ,else
            #2."""
            )

        your_answer5 =input(">")

        if your_answer5 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya5 =input(">>>>EatRice:")
            return ya5

        elif your_answer5 =="2":
            next_Step5 = input("Do other what? Please input your new thought!\n>")
            next_step5 = Choices.get(next_Step5)
            return next_step5

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'


class Sleep():

    def doing(self):
        return 'sleeping'

    def going_to_do(self):
        print("""
            Welcome come here to think about whether you ought go to sleep.
            If you aren't very sleepy, you can relax yourself by some ways.
            But if it is already late, or you need in order to move
            forward.You need to sleep right now.So ,if you have decide to
            sleep please input #1 .Else:#2."""
                 )

        your_answer6 =input(">")


        if your_answer6 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya6 =MyChoice.choices.get(input(">>>>Sleep:"))
            return ya6

        elif your_answer6 =="2":
            next_Step6 = input("Do other what? Please input your new thought!\n>")
            next_step6 = Choices.get(next_Step6)
            return next_step6

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'


class MyChoice(object):

    choices ={
        'watch_movie' : WatchMovie(),
        'play_computer' : PlayComputerGame(),
        'listen_music' : ListenMusic(),
        'go_outside' : GoOutside(),
        'eat_rice' : EatRice(),
        'sleep' : Sleep(),
        #'do_not_know_do_what' : DoWhat()  之前多做的一个类,后续调整就没有删除掉
    }

    def __init__(self, want_to_do):
        self.want_to_do =want_to_do

    def consider_again(self, other_choice):
        cho =MyChoice.choices.get(other_choice)
        return cho# cho = MyChoice.

    def first_thing(self):
        return self.consider_again(self.want_to_do)

Choices={'1' : 'watch_movie',
         '2' : 'play_computer',
         '3' : 'listen_music',
         '4' : 'go_outside',
         '5' : 'eat_rice',
         '6' : 'sleep'}

print(Choices)
you_first_choice=input("Just input you choice corresponding number.\n>")
a_software =Choices.get(you_first_choice)#拿到了第一个字典里面的某个 选项
a_guide = MyChoice(a_software) #MyChoice带着 某个选项作为属性开了
the_software_is_beginning = DoWhat(a_guide) #DoWhat(MyChoice(选项)).do()
the_software_is_beginning.do()#DoWhat(MyChoice(选项)).do()。    MyChoice(选项)就是一个object 之

            这是总文件分开后的代码,一共分了九个,最终运行的是第一个。

1st:

#from sys import exit
#from random import randint

from ex451 import DoWhat
from ex452 import WatchMovie
from ex453 import PlayComputerGame
from ex454 import ListenMusic
from ex455 import GoOutside
from ex456 import EatRice
from ex457 import Sleep
from ex458 import MyChoice






Choices={'1' : 'watch_movie',
         '2' : 'play_computer',
         '3' : 'listen_music',
         '4' : 'go_outside',
         '5' : 'eat_rice',
         '6' : 'sleep'}

print(Choices)
you_first_choice=input("Just input you choice corresponding number.\n>")
a_software =Choices.get(you_first_choice)
a_guide = MyChoice(a_software)
the_software_is_beginning = DoWhat(a_guide)
the_software_is_beginning.do()

2nd:

#from random import random
from random import randint
from sys import exit

class DoWhat(object):# (object)==(MyChoice(选项))

    def __init__(self, do_this):
        self.do_this = do_this#这两行代码大概意思就是将self.do_this去使用以 do_this为名的这个类名了,可见11行就是应用。
#        print(self.do_this)  以下有这种样式的#字符,都是为了方便理解 设置的打印,可以在终端运行的时候,看看到底发生了什么。

    def do(self):
        want_to_do_now = self.do_this.first_thing()#return的路径:240→243→10
#        print(want_to_do_now)
#   这是刚开始写的时候的错误代码,没舍得删除     really_want_to_do = self.do_this.consider_again()

        while want_to_do_now != 1:
            next_want_to_do = want_to_do_now.going_to_do()#运行 某个类里面的 going_to_do函数
            true_answer =want_to_do_now.doing()#运行某个类里面的 doing()函数,返还一个字符串,进行if语句
#            a =want_to_do_now
#            print(a,b,next_want_to_do)

            if true_answer ==next_want_to_do: #这种不是赋值的需要用两个符号啊。
                new_sentence =[
                f"\nThen you have to get seriously about {true_answer}.",
                f"\nRemember {true_answer} seriously.",
                f"\nYou don't have to think about others when you are {true_answer}.",
                f"\nWhen you finish the thing of {true_answer}, welcome you come here again!"
                ]

                print(new_sentence[randint(0,3)])#随机数表,[0,3]刚开始做了个[0,4]以为取左不取右,记混了,结果 出现了list index out of range这个问题
                exit(1)
            else:
                print("I believe your new thought is better than the other")
            want_to_do_now = self.do_this.consider_again(next_want_to_do)

3rd:

#from ex458 import MyChoice


class WatchMovie():

    def doing(self):
        return 'watching movie'

    def going_to_do(self):
        print("""
            Welcome come here to think about weather you should watch
            movie.If you have movie which you want to watch and you are
            avalible and long time no watch and you have learnt many days.
            You can watch,else you need to consider again.If you will
            watch movie please input #1 ,else #2"""
            )

        your_answer1 = input(">")

        if your_answer1 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya1 = input(">>>>watching movie:")#ya 就是your_answer的缩写,1代表的这个是第一个调用
            return ya1

        elif your_answer1 =="2":
            next_Step1 = input("Do other what? Please input your new thought!\n>")
            next_step1 = Choices.get(next_Step1)
            return next_step1

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'

Choices={'1' : 'watch_movie',
         '2' : 'play_computer',
         '3' : 'listen_music',
         '4' : 'go_outside',
         '5' : 'eat_rice',
         '6' : 'sleep'}

4th:

#from ex458 import MyChoice

class PlayComputerGame():

    def doing(self):
        return 'playing computer'

    def going_to_do(self):
        print("""
            Welcome come here to think about weather you should play
            computer game .First, if you haven't study emotion and you
            specially want to play some hero.Second ,your junior high
            school classmates(at least two) is waiting someone or your
            senior high school classmates invite you to join their team!
            If one of these conditions is met,please input #1.Else,#2."""
            )

        your_answer2 = input(">")

        if your_answer2 == "1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya2 =input(">>>>playing computer:")
            return ya2

        elif your_answer2 =="2":
            next_Step2 = input("Do other what? Please input your new thought!\n>")
            next_step2 = Choices.get(next_Step2)
            return next_step2

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'


Choices={'1' : 'watch_movie',
         '2' : 'play_computer',
         '3' : 'listen_music',
         '4' : 'go_outside',
         '5' : 'eat_rice',
         '6' : 'sleep'}

5th:

#from ex458 import MyChoice

class ListenMusic():

    def doing(self):
        return 'listening music'

    def going_to_do(self):
        print("""
            Welcome come here to think about weather you should listen
            music .If you heard it two hours ago or you have new songs
            to listen to.It's a good choice that you go to listen to
            music next.\tIf you really decide to listen to music,input
            #1 ,else:2"""
            )

        your_answer3 =input(">")

        if your_answer3 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya3 =input(">>>>listening music:")
            return ya3

        elif your_answer3 =="2":
            next_Step3 = input("Do other what? Please input your new thought!\n>")
            next_step3 = Choices.get(next_Step3)
            return next_step3

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'

Choices={'1' : 'watch_movie',
         '2' : 'play_computer',
         '3' : 'listen_music',
         '4' : 'go_outside',
         '5' : 'eat_rice',
         '6' : 'sleep'}

6th:

#from ex458 import MyChoice

class GoOutside():

    def doing(self):
        return 'going outside'

    def going_to_do(self):
        print("""
            Welcome come here to think about weather you should go
            outside.If you are very free and specially going to go outside
            as well as you haven't had a 50¥ daily bill for almost a week.
            You should go outside to enjoy the free time immediately.So ,if
            intend to going outside please input #1,else 2."""
            )

        your_answer4 =input(">")

        if your_answer4 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya4 =input(">>>>going outside:")
            return ya4

        elif your_answer4 =="2":
            next_Step4 = input("Do other what? Please input your new thought!\n>")
            next_step4 = Choices.get(next_Step4)
            return next_step4

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'

Choices={'1' : 'watch_movie',
         '2' : 'play_computer',
         '3' : 'listen_music',
         '4' : 'go_outside',
         '5' : 'eat_rice',
         '6' : 'sleep'}

7th:

#from ex458 import MyChoice

class EatRice():

    def doing(self):
        return 'eating rice'

    def going_to_do(self):
        print("""
            Welcome come here to think about whether you should eat rice.
            First, if it's almost dinner time.Second, you have been hungry
            for a long time. Third, you go to the dining hall is right at
            present! If you meet all the conditions please input #1 ,else
            #2."""
            )

        your_answer5 =input(">")

        if your_answer5 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya5 =input(">>>>eating rice:")
            return ya5

        elif your_answer5 =="2":
            next_Step5 = input("Do other what? Please input your new thought!\n>")
            next_step5 = Choices.get(next_Step5)
            return next_step5

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'


Choices={'1' : 'watch_movie',
         '2' : 'play_computer',
         '3' : 'listen_music',
         '4' : 'go_outside',
         '5' : 'eat_rice',
         '6' : 'sleep'}

8th:

#from ex458 import MyChoice

class Sleep():

    def doing(self):
        return 'sleeping'

    def going_to_do(self):
        print("""
            Welcome come here to think about whether you ought go to sleep.
            If you aren't very sleepy, you can relax yourself by some ways.
            But if it is already late, or you need in order to move
            forward.You need to sleep right now.So ,if you have decide to
            sleep please input #1 .Else:#2."""
                 )

        your_answer6 =input(">")


        if your_answer6 =="1":
            print("OK,you need to say your the answer out loud and write it down personally.")
            ya6 =input(">>>>sleeping:")
            return ya6

        elif your_answer6 =="2":
            next_Step6 = input("Do other what? Please input your new thought!\n>")
            next_step6 = Choices.get(next_Step6)
            return next_step6

        else:
            print("Please input 1 or 2!!!")
            return 'do_not_know_do_what'

Choices={'1' : 'watch_movie',
         '2' : 'play_computer',
         '3' : 'listen_music',
         '4' : 'go_outside',
         '5' : 'eat_rice',
         '6' : 'sleep'}

9th:

from ex451 import DoWhat
from ex452 import WatchMovie
from ex453 import PlayComputerGame
from ex454 import ListenMusic
from ex455 import GoOutside
from ex456 import EatRice
from ex457 import Sleep




class MyChoice(object):

    choices ={
        'watch_movie' : WatchMovie(),
        'play_computer' : PlayComputerGame(),
        'listen_music' : ListenMusic(),
        'go_outside' : GoOutside(),
        'eat_rice' : EatRice(),
        'sleep' : Sleep(),
        #'do_not_know_do_what' : DoWhat()
    }

    def __init__(self, want_to_do):
        self.want_to_do =want_to_do

    def consider_again(self, other_choice):
        cho =MyChoice.choices.get(other_choice)
        return cho

    def first_thing(self):
        return self.consider_again(self.want_to_do)

代码肯定会出现一些小小的bug,但是他也确实跑起来了。另外如果大佬们觉得我的代码可以有更简单的写法,希望也可以教教我一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值