dsaaaa12 2021-09-23 10:21 采纳率: 100%
浏览 1064
已结题

python program 要求如下

img

img

img

img

img

例子来了然后根据要求写

只用define,while,if. 图一上有例子 ,得有个main procedure,简单一点的program设计就行 像是猜翻硬币就可以了,希望可以快点谢谢 一定要跟图片上要求一样!

  • 写回答

4条回答 默认 最新

  • hyh123a 全栈领域新星创作者 2021-09-24 00:23
    关注

    写完了,可以试一下:

    游戏机制如下:
    在游戏中:
    1、同样的名字进入游戏,会进入之前的游戏积分。
    2、top down left right 可能得0分也可能得高分,随机。
    3、没见过的名字,积分为0,新开始游戏。

    注意: 选择的时候:'Y', 'N'注意大小写。

    import random
    
    
    def main():
        global points, player
        points = []
        player = []
        greet()
        return
    
    
    def greet():
        smile_face = "\U0001f600"
        print(f'{smile_face} Welcome !It is a game of throwing sieves.')
        continue_flag = True
        while continue_flag:
            name = input("You can start the game. Please input your name:('N' exit the game)")
            if name == 'N':
                print(f'{smile_face} Exit !')
                continue_flag = False
            if name in player:
                _index = player.index(name)
                _score = points[_index]
                print(f'Hello {name}, the name already exists and your score is {_score}')
                print(f'Please input your choice:')
                print(f'1: start')
                print(f'2: input your name again')
                print(f'Others: exit')
                choice = input()
                if choice == '1':
                    score = play(_score)
                    points[_index] = score
                    _exit = input("Exit the game:(Y or N)")
                    if _exit == 'Y':
                        continue_flag = False
                        print(f'Exit!')
                    else:
                        print(f'Restart!')
                        continue_flag = True
                elif choice == '2':
                    continue_flag = True
                else:
                    print(f'Exit!')
                    continue_flag = False
                    print(f'{smile_face} Exit !')
    
            else:
                print(f'Hello {name}, Your score is 0. Start')
                player.append(name)
                score = play(init_score=0)
                points.append(score)
                _exit = input("Exit the game:(Y or N)")
                if _exit == 'Y':
                    continue_flag = False
                    print(f'Exit!')
                else:
                    print(f'Restart!')
                    continue_flag = True
    
    
    def play(init_score):
        total_score = init_score
        print(f'You have {init_score}!')
        continue_flag = True
        while continue_flag:
            print(f'Please input your choice:')
            print(f'1: top')
            print(f'2: right')
            print(f'3: down')
            print(f'4: left')
            print(f'Others: exit')
            _input = input()
    
            random_dict = {
                '1': 10 * random.randint(0, 1),
                '2': 20 * random.randint(0, 1),
                '3': 30 * random.randint(0, 1),
                '4': 40 * random.randint(0, 1),
            }
            if _input in random_dict.keys():
                add_score = random_dict[_input]
                total_score += add_score
                print(f'You have selected the option:{_input}')
                print(f'You get the score :{add_score}')
                print(f'Your total score is :{total_score}')
            else:
                continue_flag = False
                print(f'Exit')
                print(f'Your total score is :{total_score}')
    
        return total_score
    
    
    if __name__ == "__main__":
        main()
    

    测试结果:

    /usr/bin/python3.8 /home/hyh/Project/MyTools/1/cyoa.py
    😀 Welcome !
    You can start the game. Please input your name:('N' exit the game)xiaozi
    Hello xiaozi, Your score is 0. Start
    You have 0!
    Please input your choice:
    1: top
    2: right
    3: down
    4: left
    Others: exit
    1
    You have selected the option:1
    You get the score :10
    Your total score is :10
    Please input your choice:
    1: top
    2: right
    3: down
    4: left
    Others: exit
    3
    You have selected the option:3
    You get the score :30
    Your total score is :40
    Please input your choice:
    1: top
    2: right
    3: down
    4: left
    Others: exit
    exit
    Exit
    Your total score is :40
    Exit the game:(Y or N)y
    Restart!
    You can start the game. Please input your name:('N' exit the game)xiaozi
    Hello xiaozi, the name already exists and your score is 40
    Please input your choice:
    1: start
    2: input your name again
    Others: exit
    1
    You have 40!
    Please input your choice:
    1: top
    2: right
    3: down
    4: left
    Others: exit
    2
    You have selected the option:2
    You get the score :0
    Your total score is :40
    Please input your choice:
    1: top
    2: right
    3: down
    4: left
    Others: exit
    3
    You have selected the option:3
    You get the score :30
    Your total score is :70
    Please input your choice:
    1: top
    2: right
    3: down
    4: left
    Others: exit
    exit
    Exit
    Your total score is :70
    Exit the game:(Y or N)y
    Restart!
    You can start the game. Please input your name:('N' exit the game)n
    Hello n, Your score is 0. Start
    You have 0!
    Please input your choice:
    1: top
    2: right
    3: down
    4: left
    Others: exit
    exit
    Exit
    Your total score is :0
    Exit the game:(Y or N)Y
    Exit!
    
    Process finished with exit code 0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月8日
  • 已采纳回答 9月30日
  • 修改了问题 9月23日
  • 修改了问题 9月23日
  • 展开全部

悬赏问题

  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)