2014-02-13 65 views
-1

我在寫程序;有沒有寫這個的有效方法?我想提高我的知識。用不同的變量寫同一個進程的有效方法

import random 
import math 

fighter_strength=random.randint(1,1000) 
fighter_skill=random.randint(1,1000) 
enemy_strength=random.randint(1,1000) 
enemy_skill=random.randint(1,1000) 




strength_difference=fighter_strength-enemy_strength 
global skill_difference 
skill_difference=fighter_skill-enemy_skill 
global strength_modifier 

if strength_difference > 0: 
    strength_modifier=math.trunc((fighter_strength-enemy_strength)/12) 
    print "You are stronger than the enemy by",strength_modifier 

else: 
    strength_modifier=math.trunc((enemy_strength-fighter_strength)/12) 
    print "You are weaker than the enemy by",strength_modifier 



if skill_difference > 0: 
     skill_modifier=math.trunc((fighter_skill-enemy_skill)/5) 
     print "You are more skilful than the enemy by",skill_modifier 


else: 
    skill_modifier=math.trunc((enemy_skill-fighter_skill)/5) 
    print "You are less skilful than the enemy by",skill_modifier 


print "you are done!" 
+0

歡迎計算器。你能澄清一下你想每次都不同的「變數」嗎? (你可能在尋找'sys.argv',但也許我猜猜錯了)。 – alexis

+0

這將是strength_modifier和skill_modifier作爲不同的過程完成,雖然它不是一個變量 – user3298793

+0

'global'在這段代碼中沒有做任何事情。 –

回答

2

您當然可以減少重複,爲力量:

strength_difference = fighter_strength - enemy_strength 
strength_modifier = math.trunc(abs(strength_difference)/12) # note abs 
print "You are {0} than the enemy by {1}".format("weaker" if strength_difference < 0 else "stronger", 
               strength_modifier) 

你也可以考慮類和對象,例如,作爲一個非常簡單的開始:

def RandomFighter(object): 

    def __init__(self): 
     self.strength = random.randint(1, 1000) 
     self.skill = random.randint(1, 1000) 

player = RandomFighter() 
enemy = RandomFighter() 

strength_difference = player.strength - enemy.strength 
... 

最後,使用global變量一般是可以改進的空間的跡象...

+0

待定公平的,'全球'的聲明,因爲他使用它們,實際上並沒有做任何事情,但你的觀點仍然是很好的;-) –

1

顯然你有兩個物體(figher和敵人)與你想比較的屬性(力量,技能)。所以你需要一個具有兩種比較方法的類:

import random 

class Combatant: 
    def __init__(self): 
     self.strength = random.randint(1,1000) 
     self.skill = random.randint(1,1000) 

    """ 
    True if instance has more or equal strength 
    False if challenger has more strength 
    """ 
    def compare_strength(self, challenger): 
     return self.strength >= challenger.strength 

    """ 
    True if instance has more or equal skill 
    False if challenger has more skill 
    """ 
    def compare_skill(self, challenger): 
     return self.skill >= challenger.skill 


def main(): 
    left = Combatant() 
    right = Combatant() 
    print 'left st=', left.strength, ', sk=', left.skill 
    print 'right st=', right.strength, ', sk=', right.skill 

    print left.compare_strength(right) 
    print left.compare_skill(right) 


main() 
+0

THX每個人都爲偉大的答案你打開我的想法,新代碼,我哪我真的很棒。 – user3298793

+0

如果你對我的回答感到滿意,請將它記下來,並將其標記爲已接受的答案;) – tjati

0

完成此方法,2或3行。我不會爲你做這件事,如果你做你自己,你會學到更多。然後,當看完數學,那裏有一些重複。您可以定義哪些功能來幫助您。只需一次重構一步,直到完成。

def report(difference , betterString, worseString, denominator): 
    … 

然後

report(strength_difference, "stronger", "weaker", 12); 
report(skill_difference, "more skilful", "less skilful", 5); 
+0

我需要等級15幫助你的答案理查德,你能完全寫出來,因爲我真的不明白你說的我只是在說什麼十年 – user3298793

相關問題