Python 有人能帮我改进一下代码吗?(我是新手)

Python 有人能帮我改进一下代码吗?(我是新手),python,Python,只是想一个专业人士的看法,如果你编辑这个提前感谢。 (如果你这样做的话)我希望这段代码能集中在同一个主题上(作为一个地下城游戏)。感谢你花时间来看这个 import time import random print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") print("Text dungeon explorer!") print("COMMANDS:") print("inventory -- ac

只是想一个专业人士的看法,如果你编辑这个提前感谢。 (如果你这样做的话)我希望这段代码能集中在同一个主题上(作为一个地下城游戏)。感谢你花时间来看这个

import time 
    import random

    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print("Text dungeon explorer!")
    print("COMMANDS:")
    print("inventory -- acceses your current inventory")
    print("hold 'item' -- hold selected item")
    print("attack -- attacks if enemy is approched")
    print("eat -- eats current item held")
    print("use -- uses item held")
    print("open -- opens any available chests in said room")
    print("pickup -- picks up any current items in said room")
    print("drop -- drops held item")
    print("throw -- throws held item")
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

    time.sleep(3)

    print ("It's a dark in the dungeon, no food, no wepons. You don't even know where you are... There is an object on the floor, all you can make out of it is that it looks quite like a stick.")

    time.sleep(11)

    ch1 = str(input("Do you take it? [y/n]: "))

    if ch1 in ['y', 'Y', 'Yes', 'YES', 'yes']:
        print("You have taken the stick! A very odd feel to it.")
        time.sleep(2)
        backpack_stick = 1

    else:
          print ("You did not take the stick.")
          backpack_stick = 0

    print ("As you proceed further into the cave, you see a wooden door.")
    time.sleep(6)
    ch2 = str(input("Do you open the door? [y/n]"))

    enmysleep_rat = int(random.randomint(1, 10))
    enmychn_rat = int(random.randomint(1, 10))
    chstchnc = int(random.randomint(1, 10))
    if enmychn_rat == 1 or 2 or 3 or 4:
      enmychance_rat = True
      if enmychance_rat is True:
         print("Oh no! A musipial rat! You take a closer look if it is sleeping.")

    time.sleep(3)
             if enmychance_rat is True and
        enmysleep_rat = 1 or 2:

             print("The rat is sleeping! I could take the availible loot without fighting!")
    elif enmysleep_rat = 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10:
             print("The rat is not sleeping! Prepare for battle!")
一,。可以替换为以下内容(注意:如果你写一些像“不”这样的字来表示不,我的建议将失败,因为它里面有一个“y”)。编辑:其他人建议使用input.lower().startsWith(“y”),这比我建议的要好,如果您键入“不”,也不会失败

if 'y' in ch1.lower():
二,。而且

if enmychn_rat == 1 or 2 or 3 or 4:
可以用

if enmychn_rat in [1, 2, 3, 4]:
我很确定你原来的路线不会达到你的目的。它评估为

if enmychn_rat == 1 or True or True or True:
这总是正确的

if enmychance_rat is True
可以改为简单

if enmychance_rat

小心,不要测试门的ch2变量。试着做一些定义。 做一些全面的案例,比如将来你可能会有越来越多的“动物”像老鼠。 对于一个“真实”的游戏感觉,尝试随机事件,比如,如果你有一个“动作”,比如开门,你是否采取了行动等等,你可以为他们指定事件

制造更多的生物,像这样

随机输入

def creature():
    creatures = ["rat","pig","parrot","monster"]
    life = random.randint(1,10)
    is_asleep_var = random.randint(1,6)
    if is_asleep_var == 1:
        is_asleep = "sleeping"
    else:
        is_asleep = "awake"
    print ("Oh a " + creatures[random.randint(0,len(creatures)-1)]+" with "+str(life)+" life, who is " + str(is_asleep))

while(1):

    creature()
    raw_input()

制造越来越多的事件,生物。事件可以触发生物。你应该调查对象。制造一个角色对象,一个怪物对象,给他们增加生命,攻击点等,制造等级

您遇到问题的代码是否存在特定问题?如果有一些特定的问题您无法解决,您可以使用一个简短的代码示例来重现这个问题。正如目前所问,如果它的一些建议没有具体问题,那么这是一个过于宽泛的代码审查:a)您不必将
输入
转换为字符串;它已经是了,b)
backpack\u stick=0
这种跟踪物品的方法效率很低,因为它会产生太多的变量。改为使用dict
backpack={'stick':0}
。c)
randomint
返回一个整数。同样,无需转换。d)
如果enmychn\u rat==1或2或3或4:
不做您认为它做的事fix indentation,因为这是python中的一个关键思想,可能会导致未被注意到但灾难性的行为。谢谢!我真的没想到会有人回复,因为我知道论坛对这类东西并不友好。
def creature():
    creatures = ["rat","pig","parrot","monster"]
    life = random.randint(1,10)
    is_asleep_var = random.randint(1,6)
    if is_asleep_var == 1:
        is_asleep = "sleeping"
    else:
        is_asleep = "awake"
    print ("Oh a " + creatures[random.randint(0,len(creatures)-1)]+" with "+str(life)+" life, who is " + str(is_asleep))

while(1):

    creature()
    raw_input()