只是想要一个专业人士对此视图,提前感谢如果你编辑这个。 (如果你这样做)我想这段代码专注于同一主题(作为地下城游戏)。感谢您甚至花时间看这个!有人可以请我改进我的代码吗? (我是一个新手)
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!")
您遇到问题的代码是否存在特定问题?如果有什么特定的东西你不能弄清楚,你可以要求使用一个简短的代码来重现问题。正如目前所问,这太宽了 – Kewl
尝试代码审查,如果没有具体问题 – depperm
一些推荐:a)你不必将'输入'转换为字符串;它已经是和b)'backpack_stick = 0'这种跟踪项目的方法效率非常低,因为它创建了太多的变量。用dict代替'backpack = {'stick':0}'。 c)'randomint'返回一个整数。再次不需要转换。 d)'如果enmychn_rat == 1或2或3或4:'不会做你认为它的做法 –