【问题标题】:I created a basic python project since im just now learning it any advice on a project to make next? [closed]我创建了一个基本的 python 项目,因为我刚刚学习它,对接下来要制作的项目有什么建议吗? [关闭]
【发布时间】:2022-10-23 12:08:15
【问题描述】:

有人告诉我,制作项目是学习用语言编写代码而不是复制和粘贴教程的最佳方式。下面的代码是我的项目,因此您可以了解我的基本技能水平以及我需要改进的地方。谢谢

import random
bullet = random.randrange(1, 6)
print("Hello welcome to Russian Roullete\nenter 'shoot' to shoot the current slot or 'cock' to rotate the revolver")
choice = input()
if choice == ("shoot"):
    if bullet == 1:
        print("you are dead heheheha")
    else:
        print("you survived")
elif choice == ("cock"):
    print("cock or shoot")
    choice2 = input()
    if choice2 == ("shoot"):
        if bullet == 2:
            print("you are dead heheheha")
        else:
                print("you survived")
    elif choice2 == ("cock"):
        print("cock or shoot")
        choice3 = input()
        if choice3 == ("shoot"):
            if bullet == 3:
                print("you are dead heheheha")
            else:
                print("you survived")
        elif choice3 == ("cock"):
            print("cock or shoot")
            choice4 = input()
            if choice4 == ("shoot"):
                if bullet == 4:
                    print("you are dead heheheha")
                else:
                    print("you survived")
            elif choice4 == ("cock"):
                print("cock or shoot")
                choice5 = input()
                if choice5 == ("shoot"):
                    if bullet == 5:
                        print("you are dead heheheha")
                    else:
                        print("you survived")
                elif choice5 == ("cock"):
                    print("last one cock or shoot")
                    choice6 = input()
                    if choice6 == ("shoot"):
                        if bullet == 6:
                            print("you are dead heheheha")
                        else:
                            print("you survived")
                    elif choice6 == ("cock"):
                        print("you coward") 

【问题讨论】:

  • 欢迎来到堆栈溢出。你面前有一个伟大的下一个项目。您编写代码的方式可能有效,但编写方式效率低下。每当您发现自己一遍又一遍地重复几乎相同的代码时,通常会有更好的方法来构建该代码。作为下一个项目,我建议你弄清楚如何在你的程序中添加一个循环,这样你就可以摆脱所有的重复。无论您的用户正在翘起和射击的枪中有多少个气缸,您的代码应该几乎无需修改即可工作。

标签: python console console-application


【解决方案1】:

这是一个不错的作品,但对于俄罗斯轮盘来说,线条太多了。 如果我想写这样的代码,我更喜欢定义像def cock()while Loop 这样的函数,这样就不会每次都重复相同的过程,使代码更短,也让它运行到第5个子弹。不要误会我的意思,代码写得很好,这里的问题是流程图。 你可以像这样使用 while 循环来改进它:

import random as ran
play_counts = 0
while True:
    print("
******Hello Welcome To Russian Roullete******
") if play_counts < 1 else
        print("
******Ready To Try Again? Lets Find Out******
")

    shotx = ran.randrange(1,6)
    bullet_slot = ran.randrange(1,6)

    while True:

        choice = input("
Press '1' to shoot the current slot 
        
Press '2' to rotate the revolver
What would be your choice: ")

        if choice == "1":

            if shotx == bullet_slot:
                rematch = input("you're dead =(
 Rematch? (Yes/No): ")
                if rematch  == "no" or rematch == "No":
                    exit()
                else:
                    break
            else:

                print(shotx)
                if shotx < 6:
                    shotx += 1
                else:
                    shotx = 1
                print("you shot and survived")
                
        elif choice == "2":     
            if shotx < 6:
                shotx += 1
            else:
                shotx = 1

        else:
            print("
----you can only choose between 1 and 2----")

    play_counts += 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    相关资源
    最近更新 更多