如何在Python 2.7中统计转数

2024-06-08 17:14:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要一个只对探索和狩猎选项计1圈的回合计数器。再加上现在的年龄。我该怎么做?我使用的是python2.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()

Tags: nameimportagesizeifoscurrentmenu
2条回答

初始化turn=0

在探索/狩猎案例中,添加以下内容:

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

可能必须将age定义为int,因为它当前是一个字符串,但一旦您这样做,此代码将完成任务。你知道吗

while代码集中,将其设置为0。你知道吗

while loop == 1:
    turn = 0
    menu()
    # ...

因为你也在增加年龄,所以最好把"current age"设为0,而不是"0"。 在狩猎和探索if子句中,按1递增"current age"turn。(改用elif会更像Python)

elif choice == 2:
    turn += 1
    kenyan_sand_boa["current age"] += 1
    # exploring
elif choice == 3:
    turn += 1
    kenyan_sand_boa["current age"] += 1
    # hunting

相关问题 更多 >