2017-10-20 27 views
0

我想要一個轉數計數器,只計算探索和狩獵選項1轉。並將其添加到當前時代。我怎樣才能做到這一點?我使用python 2.7。下面的代碼的重要部分:如何計算蟒蛇中的轉數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

trun = 0; ..... if .... turn + = 1? –

+0

謝謝。這是完美的。 –

+0

在頂部有'INFO = 1','EXPLORING = 2','HUNTING = 3'和'INVENTORY = 4'可能很有用。這將允許你做'如果選擇== INFO:'等等。另外,你可以把所有的'if'在第一個之後改爲'elif's。 – user2471379

回答

1

初始化turn=0

在你探索/亨特的情況下,添加以下內容:

turn = turn+1 
kenyan_sand_boa["current age"] = kenyan_sand_boa["current age"]+1 

可能需要界定年齡爲int,如它目前是一個字符串,但是一旦你這麼做,這段代碼就會完成工作。

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