有趣的项目:创建一个简单的OSRS钓鱼计算器

2024-05-28 10:35:29 发布

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

在我描述代码之前,这是它的基础。 Old School Runescape Fishing XP Table

该程序应该能够询问用户他们捕获了多少种鱼,告诉程序他们捕获了哪些鱼,并询问他们捕获了多少种特定的鱼。一旦程序有了所有的输入,它就应该 1.计算从该特定鱼获得的总经验值。 2.显示每捕获总量每条鱼获得的XP。 3.显示一个条形图,显示每条鱼捕获的鱼的总量

我正在制作一个并排的条形图,它不仅会显示特定鱼的捕获总量,还会显示与之相关的XP比率

代码在下面

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import itertools

def fish_menu():
    print('Type out the name of the fish.')
    print('In the proper order once the prompt comes up.')
    print('1. Tuna')
    print('2. Lobster')
    print('3. Swordfish')
    print('4. Monkfish')
    print('5. Shark')

#Once the fish is determined, it will print out the XP totals.
def draw_bargraph():

    y_pos = np.arange(len(fishes2))
    #Create bar graph
    plt.bar(y_pos, caught2)
    #Names of axes
    plt.xticks(y_pos, fishes2)
    #Show graph
    plt.show()

def fish_calc():
    while len(fishes) > 0:
        if 'Tuna' in fishes:
            print('Catching {0} Tuna is {1} total fishing XP'.format(caught[i], caught[i]*80))
        elif 'Lobster' in fishes:
            print('Catching {0} Lobster is {1} total fishing XP'.format(caught[i], caught[i]*90)) 
        elif 'Swordfish' in fishes:
            print('Catching {0} Swordfish is {1} total fishing XP'.format(caught[i], caught[i]*100)) 
        elif 'Monkfish' in fishes:  
            print('Catching {0} Monkfish is {1} total fishing XP'.format(caught[i], caught[i]*120)) 
        elif 'Shark' in fishes:
            print('Catching {0} Shark is {1} total fishing XP'.format(caught[i], caught[i]*110)) 
        fishes.pop(0)
        caught.pop(0)       

def fish_calc2():
    if 'Tuna' in fishes:
        tunaxp = caught[a] * 100
        print('Catching {0} Tuna is {1} fishing XP.'.format(caught[a], tunaxp))

if __name__ == '__main__':
    try:    
        fish_menu()
        types_of_fish = int(input('How many kinds of fish did you catch?: '))
        fishes = []
        caught = []
        fishes2 = []
        caught2 = []
        if types_of_fish < 6:
            for i in range(1, types_of_fish+1): 
                which_fish = input('What was fish number {0}: '.format(i))
                catch = int(input('How many did you catch of {0}: '.format(which_fish)))
            #Adding the fish type and total amount into the empty lists
                fishes.append(which_fish)
                caught.append(catch)
                fishes2.append(which_fish)
                caught2.append(catch)
            #We are able to at least have the first fish entry be calculated
            #Order matters right now
            for i in range(types_of_fish):
                fish_calc()

        else:
            print('Input exceeds number of options.')
    except ValueError:
        print('Invalid input.')
    else:
        draw_bargraph()

我遇到的一些问题是,如果用户没有按照我在fish_calc()函数中为它们设定的特定顺序输入鱼会怎么样。我使用while循环来防止在fishes.pop(0)函数旁边连续执行。比如说,

###Fish menu that had to be created for it to work.###
###I want to remove the second line about "proper order"
Type out the name of the fish.
In the proper order once the prompt comes up.
1. Tuna
2. Lobster
3. Swordfish
4. Monkfish
5. Shark

###User input###
How many kinds of fish did you catch?: 2
What was fish number 1: Swordfish
How many did you catch of Swordfish: 10
What was fish number 2: Tuna
How many did you catch of Tuna: 3

###Output###
Catching 10 Tuna is 800 total fishing XP
Catching 3 Tuna is 240 total fishing XP

它认识到“金枪鱼”在列表中,并对“金枪鱼”执行XP计算,而实际上它是指“箭鱼”。然而,柱状图是正确的。 True BarGraph With False Results

但是,如果您按顺序输入鱼,您将始终收到正确的结果

Type out the name of the fish.
In the proper order once the prompt comes up.
1. Tuna
2. Lobster
3. Swordfish
4. Monkfish
5. Shark
How many kinds of fish did you catch?: 3
What was fish number 1: Lobster
How many did you catch of Lobster: 5
What was fish number 2: Swordfish
How many did you catch of Swordfish: 15
What was fish number 3: Shark
How many did you catch of Shark: 10

Catching 5 Lobster is 450 total fishing XP
Catching 15 Swordfish is 1500 total fishing XP
Catching 10 Shark is 1100 total fishing XP

图表也是正确的。 True Graph with True Results

以前,如果没有if __name__ == '__main__':部分中的for i in range(types_of_fish):,它甚至无法使用fish_calc()函数。现在,我只想完善代码,使其对用户输入更加友好


Tags: oftheinisxptotalprinttuna
1条回答
网友
1楼 · 发布于 2024-05-28 10:35:29

你们可以做一个fish exp的字典,若它只是一个常量的话。伪编码:

fishMap = {"Tuna": 80, "Lobster": 90, ...}

然后在fish_calc()中,您就不必创建if/else语句,这些语句本身是有序的:

def fish_calc():
    for i in range(len(fishes)):
        # check if value is in the dictionary first
        fish = fishes[i]
        numFish = caught[i] 
        print('Catching {0} {1} is {2} total fishing XP'.format(numFish, fish, numFish*fishMap[fish]))

当然,这也不是完全不区分大小写的

相关问题 更多 >