if / elif之后如何打印相同的randomint?

时间:2016-08-24 03:51:02

标签: python-2.6

我想让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!'

3 个答案:

答案 0 :(得分:0)

尝试在if / else语句之前保存到变量:

number = randomchance();
if (number >= 35): print 'you select a ', number

答案 1 :(得分:0)

@Konstantin有正确的答案,可以在测试所有条件之前选择一次随机数。

关于你的第二个问题(如何使用更少的代码来测试所有条件),请看下面的重写/清理。特别是,我用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: ')。

我无法忍受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!" ,所以我做了#34;是的&#34;退出而不是。 : - )

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
};
void reverse(struct node*);
void main()
{
    struct node *a;
    char ch;
    struct node *temp;
    struct node *temp1;
    a=NULL;
    clrscr();
    do
    {
    if(a==NULL)
    {
        temp=(struct node*)malloc(sizeof(struct node));
        printf("Enter Data");
        scanf("%d",&temp->data);
        temp->next=NULL;
        a=temp;
    }
       else
    {
    temp=(struct node*)malloc(sizeof(struct node));
    temp->next=NULL;
    printf("Enter data element");
    scanf("%d",&temp->data);
    temp1=a;
    while(temp1->next!=NULL)
        {
        temp1=temp1->next;
        }
    temp1->next=temp;
    }
    printf("Do You Wish to continue");
    ch=getch();
    }
    while(ch=='Y'||ch=='y');
    printf("Status of the link list");
    temp1=a;
    while(temp1!=NULL)
    {
    printf("%d ",temp1->data);
    temp1=temp1->next;
    }
    reverse(a);
    getch();
}
void reverse(struct node *head)
{
struct node *prev,*current,*next,*t;
current=head;
prev=NULL;
while(current!=NULL)
    {
    next=current;
    current->next=prev;
    prev=current;
    current=next;
    }
head=prev;
printf("Displaying in reverse order");
t=head;
while(t!=NULL)
    {
    printf("%d",t->data);
    t=t->next;
    }

}

答案 2 :(得分:-1)

我就是这样做的:

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"