如何在Python中为random.randint添加异常?

时间:2017-02-08 07:36:14

标签: python random

所以我有一个变量可以创建0到10之间的随机数,但是,我不希望创建的随机数为5.我如何在Python中添加random.randint的异常?我下面的内容不是这样做的:

number = random.randint(0, 10) !=5

这只是根据随机数是否等于5而返回True或False ...我该如何解决这个问题?

4 个答案:

答案 0 :(得分:6)

你可以做到

number = random.randint(0,10)
while number == 5:
   number = random.randint(0,10)

答案 1 :(得分:2)

怎么样

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define NAMESTRLEN 16

int main(void) {
    char str_name[NAMESTRLEN] = {'\0'};
    size_t slen;

    printf("Please enter your first name to begin: ");
    if (fgets(str_name, NAMESTRLEN, stdin) == NULL) {
        fprintf( stderr, "Error from fgets()\n");
        return 1;
    }

    slen = strlen(str_name);
    if (slen > 0 && str_name[slen-1] == '\n') {
        str_name[slen-1] = '\0';
    } else {
        fprintf( stderr, "Too many characters\n");
        return 1;
    }

    if (str_name[0] == '\0') {
        fprintf( stderr, "No name entered\n");
        return 1;
    }

    str_name[0] = toupper((unsigned char)str_name[0]);

    printf("Hi %s.\n", str_name);

    return 0;
} 

单线

答案 2 :(得分:1)

如果您确实想要引发错误,则可以使用assert

number = random.randint(0, 10)
assert number != 5
如果满足您的条件,

raise会出错。

number = random.randint(0, 10)
if number == 5:
    raise ValueError # or another Exception of choice

或者如果你想继续尝试,直到得到一个非5的随机数,那么

while True:
    number = random.randint(0, 10)
    if number != 5:
        break

答案 3 :(得分:0)

!=实际上是一个返回true或false的运算符 试试这个:

public class ToDoCursorAdapter extends CursorAdapter {
List<Integer> selectedArabTextItemsPositions;//to store all selected items position

Context context;
public ToDoCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
    selectedArabTextItemsPositions = new ArrayList<>();
    this.context = context;
}

// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.activity_view_record, parent, false);
}
@Override
public View getView(int position, View convertview, ViewGroup arg2) {
    if (convertview == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertview = inflater.inflate(R.layout.activity_view_record,
                null);
    }
    convertview.setTag(position);
    return super.getView(position, convertview, arg2);
}



@Override
public void bindView(final View view, Context context, final Cursor cursor) {

    Log.d("TEST",String.valueOf((Integer) view.getTag()));

    final TextView arabText;
    final TextView engText;
    final TextView refText;
    final Button button;
    final Button button2;

    engText = (TextView) view.findViewById(R.id.engText);
    arabText = (TextView) view.findViewById(R.id.arabText);
    refText = (TextView) view.findViewById(R.id.refText);
    button = (Button) view.findViewById(R.id.button);
    button2 = (Button) view.findViewById(R.id.button2);

    String arabic = cursor.getString(cursor.getColumnIndexOrThrow("PlainArab_Text"));
    String english = cursor.getString(cursor.getColumnIndexOrThrow("PlainEng_Text"));
    String ref = cursor.getString(cursor.getColumnIndexOrThrow("REF"));

    arabText.setText(arabic);
    engText.setText(english);
    refText.setText(ref);

    if(selectedArabTextItemsPositions.contains((Integer) view.getTag())) {
       arabText.setVisibility(View.GONE);
    }
    else {
        arabText.setVisibility(View.VISIBLE);
    }


    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v){
            if (selectedArabTextItemsPositions.contains((Integer) view.getTag())){
                arabText.setVisibility(View.VISIBLE);
                Log.d("selected contain id>>>>>>>>",String.valueOf(String.valueOf((Integer) view.getTag())));
                removeItemFromList((Integer) view.getTag(),selectedArabTextItemsPositions);
            }else {
                arabText.setVisibility(View.GONE);
                Log.d("selected text does not contain id>>>>>>>>",String.valueOf((Integer) view.getTag()));
                selectedArabTextItemsPositions.add((Integer) view.getTag());
            }
            Log.d("LIST Content >>>>",String.valueOf(selectedArabTextItemsPositions));
        }

    });
    button2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v){
            //viewHolder.engText.setVisibility(View.GONE);
        }
    });
}

void removeItemFromList (int object,List arrayList){
    Iterator<Integer> it = arrayList.iterator();
    while (it.hasNext()) {
        if (it.next() == object) {
            it.remove();
        }
    }
}
}