如何在 if/elif 之后打印相同的 randomint?
how to print same randomint after if/elif?
我想让 randint 每次执行时都像 if/elif 语句一样打印出来,而不返回新的整数。还有另一种方法可以不用在 while 语句之后写那么多代码吗?
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 to CS GO case lottery'
for skin in skins:
print "Available", skin,
for 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' to open a 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 you like to exit? Enter no for exit: ')
if resp1 == 'no' or "No":
exit()
else:
print "Yes or No. Answers only"
else:
print 'You\'ve run out of keys!'
尝试在 if/else 语句之前保存到变量:
number = randomchance();
if (number >= 35): print 'you select a ', number
我会这样做:
knives = ['karambit', 'flip', 'bowie', 'butterfly', 'm9 bayonet', 'bayonet', 'daggers', 'falchion']
skins = ['blue'] * 35 + ['purple'] * 20 + ['pink'] * 10 + ['red'] * 5 + [0]
skin = random.choice(skins)
print "You won a " + skin + " skin" if skin else "You won a " + random.choice(knives) + " knife"
@Konstantin 对如何在测试所有条件之前只选择一次随机数给出了正确答案。
关于你的第二个问题(如何使用更少的代码来测试所有条件),请看下面我的rewrite/cleanup。特别是,我使用random.choice
来挑选一把刀,我使用了一系列阈值(35、20等)来挑选皮肤。
我还修复了一些错误,比如你的 if resp1 == 'no' or "No":
应该是 if resp1 == 'no' or resp1 == "No":
(但我用的是 resp1.lower()
)。
我受不了 'Would you like to exit? Enter no for exit: '
,所以我 "yes" 退出了。 :-)
import random
skins = ['blue', 'purple', 'pink', 'red']
knives = ['karambit', 'flip', 'bowie', 'butterfly', 'm9 bayonet', 'bayonet', 'daggers', 'falchion']
print 'Welcome to 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)'
for _ in range(10):
resp = raw_input("Enter 'yes' to open a case: ")
while resp.lower() != 'yes':
if resp.lower() == 'no':
if raw_input('Would you like to exit? ').lower() == 'yes':
exit()
else:
print "Yes or no answers only."
resp = raw_input("Enter 'yes' to open a case: ")
print 'Opening case...'
chance = random.randint(1, 100)
for i, n in enumerate([35, 20, 10, 5]):
if chance >= n:
print "You've won a %s skin." % skins[i]
break
if chance < 5:
print "You've won a %s knife." % random.choice(knives)
print "You've run out of keys!"
我想让 randint 每次执行时都像 if/elif 语句一样打印出来,而不返回新的整数。还有另一种方法可以不用在 while 语句之后写那么多代码吗?
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 to CS GO case lottery'
for skin in skins:
print "Available", skin,
for 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' to open a 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 you like to exit? Enter no for exit: ')
if resp1 == 'no' or "No":
exit()
else:
print "Yes or No. Answers only"
else:
print 'You\'ve run out of keys!'
尝试在 if/else 语句之前保存到变量:
number = randomchance();
if (number >= 35): print 'you select a ', number
我会这样做:
knives = ['karambit', 'flip', 'bowie', 'butterfly', 'm9 bayonet', 'bayonet', 'daggers', 'falchion']
skins = ['blue'] * 35 + ['purple'] * 20 + ['pink'] * 10 + ['red'] * 5 + [0]
skin = random.choice(skins)
print "You won a " + skin + " skin" if skin else "You won a " + random.choice(knives) + " knife"
@Konstantin 对如何在测试所有条件之前只选择一次随机数给出了正确答案。
关于你的第二个问题(如何使用更少的代码来测试所有条件),请看下面我的rewrite/cleanup。特别是,我使用random.choice
来挑选一把刀,我使用了一系列阈值(35、20等)来挑选皮肤。
我还修复了一些错误,比如你的 if resp1 == 'no' or "No":
应该是 if resp1 == 'no' or resp1 == "No":
(但我用的是 resp1.lower()
)。
我受不了 'Would you like to exit? Enter no for exit: '
,所以我 "yes" 退出了。 :-)
import random
skins = ['blue', 'purple', 'pink', 'red']
knives = ['karambit', 'flip', 'bowie', 'butterfly', 'm9 bayonet', 'bayonet', 'daggers', 'falchion']
print 'Welcome to 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)'
for _ in range(10):
resp = raw_input("Enter 'yes' to open a case: ")
while resp.lower() != 'yes':
if resp.lower() == 'no':
if raw_input('Would you like to exit? ').lower() == 'yes':
exit()
else:
print "Yes or no answers only."
resp = raw_input("Enter 'yes' to open a case: ")
print 'Opening case...'
chance = random.randint(1, 100)
for i, n in enumerate([35, 20, 10, 5]):
if chance >= n:
print "You've won a %s skin." % skins[i]
break
if chance < 5:
print "You've won a %s knife." % random.choice(knives)
print "You've run out of keys!"