python 2.6 - how to print same randomint after if/elif? -
i want randint printed same in if/elif statements each time executes without new integer returning. there way instead of having write code after while statement or fine?
from random import * def randomchance(): return randint(1, 100) def randomknife(): return randint(1, 8) skins = ['blue', 'purple', 'pink', 'red'] knives = ['karambit', 'flip', 'bowie', 'butterfly', 'm9 bayonet', 'bayonet', 'daggers', 'falchion'] print 'welcome cs go case lottery' skin in skins: print "available", skin, knife in knives: print "available", knife, print '(blue = common, purple = uncommon, pink = rare, red = epic)' keys = 10 while keys >0: resp=raw_input("enter 'yes' open case: ") if (resp == str('yes') or resp == str('yes')): print 'opening case...' if (randomchance() >= 35): print 'you\'ve won a', skins[0] elif (randomchance() >= 20): print 'you\'ve won a', skins[1] elif (randomchance() >= 10): print 'you\'ve won a', skins[2] elif (randomchance() >= 5): print 'you\'ve won a', skins[3] elif (randomchance() >= 1): if randomknife == 1: print 'you\'ve won a', knifes[0] elif randomknife() == 2: print 'you\'ve won a', knifes[1] elif randomknife() == 3: print 'you\'ve won a', knifes[2] elif randomknife() == 4: print 'you\'ve won a', knifes[3] elif randomknife() == 5: print 'you\'ve won a', knifes[4] elif randomknife() == 6: print 'you\'ve won a', knifes[5] elif randomknife() == 7: print 'you\'ve won a', knifes[6] elif randomknife() == 8: print 'you\'ve won a', knifes[7] keys -= 1 elif(resp == str('no') or resp==str('no')): resp1=raw_input('would exit? enter no exit: ') if resp1 == 'no' or "no": exit() else: print "yes or no. answers only" else: print 'you\'ve run out of keys!'
@konstantin has right answer how choose random number once before testing conditions.
as second question (how use less code test conditions), take @ rewrite/cleanup below. in particular, used random.choice
pick knife, , used list of thresholds (35, 20, etc.) pick skins.
i fixed couple bugs, if resp1 == 'no' or "no":
should if resp1 == 'no' or resp1 == "no":
(but used resp1.lower()
instead).
and couldn't stand 'would exit? enter no exit: '
, made "yes" exit instead. :-)
import random skins = ['blue', 'purple', 'pink', 'red'] knives = ['karambit', 'flip', 'bowie', 'butterfly', 'm9 bayonet', 'bayonet', 'daggers', 'falchion'] print 'welcome cs go case lottery' print "available skins: %s" % ', '.join(skins) print "available knives: %s" % ', '.join(knives) print '(blue = common, purple = uncommon, pink = rare, red = epic)' _ in range(10): resp = raw_input("enter 'yes' open case: ") while resp.lower() != 'yes': if resp.lower() == 'no': if raw_input('would exit? ').lower() == 'yes': exit() else: print "yes or no answers only." resp = raw_input("enter 'yes' open case: ") print 'opening case...' chance = random.randint(1, 100) i, n in enumerate([35, 20, 10, 5]): if chance >= n: print "you've won %s skin." % skins[i] break if chance < 5: print "you've won %s knife." % random.choice(knives) print "you've run out of keys!"
Comments
Post a Comment