我想要一个仅用于探索和寻找选项的转弯计数器。并将它添加到当前时代。我怎样才能做到这一点?我正在使用python 2.7。以下是代码的重要部分:
from main import name
import os
import random
kenyan_sand_boa = {
"snek": "kenyan sand boa",
"prey": "mice, " "birds, " "lizards",
"biome": "desert",
"predators": "desert monitor lizard",
"adult age": 156,
"baby size": 10,
"adult size": 20,
"current size": 10,
"current age": "0",
"name": name,
}
os.system('cls')
def menu():
os.system('cls')
print '''Now that you have chosen what snek to be, you have a couple
options
1. Read info about kenyan sand boas.
2. Explore.
3. Hunt.
4. View inventory.
5. Save.
6. Exit.
'''
loop = 1
choice = 0
while loop == 1:
menu()
choice = int(raw_input())
if choice == 1:
#information about kenyan sand boas
if choice == 2:
#exploring
if choice == 3:
#huntinng
if choice == 4:
#view inventory
else:
menu()
答案 0 :(得分:1)
初始化turn=0
在您的探索/搜寻案例中,添加以下内容:
turn = turn+1
kenyan_sand_boa["current age"] = kenyan_sand_boa["current age"]+1
可能必须将年龄定义为int,因为它当前是一个字符串,但是这样的代码将在您完成后完成工作。
答案 1 :(得分:1)
在while
代码集中,转为0。
while loop == 1:
turn = 0
menu()
# ...
由于您也在增加年龄,因此最好将"current age"
设置为0
而不是"0"
。
在您的狩猎和探索"current age"
条款中,将turn
和if
增加一个。 (使用elif
代替更多pythonic)
elif choice == 2:
turn += 1
kenyan_sand_boa["current age"] += 1
# exploring
elif choice == 3:
turn += 1
kenyan_sand_boa["current age"] += 1
# hunting