我在寫程序;有沒有寫這個的有效方法?我想提高我的知識。用不同的變量寫同一個進程的有效方法
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!"
歡迎計算器。你能澄清一下你想每次都不同的「變數」嗎? (你可能在尋找'sys.argv',但也許我猜猜錯了)。 – alexis
這將是strength_modifier和skill_modifier作爲不同的過程完成,雖然它不是一個變量 – user3298793
'global'在這段代碼中沒有做任何事情。 –