Python - 头部或尾部练习

时间:2015-12-29 01:17:08

标签: python

我需要一些小技巧来完成这项练习。 在for循环生成0和1序列后,程序应显示head或tail。该模块包含许多随机数生成器:

import random

# function to determine head or tail of printed random number
def flip(coin):
    # if the coin is zero, then print head
    if coin == 0:
        print("heads!")
    # else print tail
    else:
        print("Tail!")
# simple repeat loop that calls flip() 10 times to generate a random
# sequence of 10 heads and tails
def main():
     for coin in range(10):
        print (random.randrange(2))

main()

2 个答案:

答案 0 :(得分:3)

你需要调用 flip函数,并传递random.randrange(2)调用的结果:

print (flip(random.randrange(2)))

正如Padraic在注释中指出的那样,您的代码也会输出None,因为它会从被调用的函数中打印返回的值,并且没有return语句产生隐式{{1 }}。您可以通过以下两种方式之一解决此问题:要么None返回结果而不是打印结果,要么不打印就调用它。

flip

或:

if coin == 0:
    return "heads!"
# else print tail
else:
    return "Tail!"

答案 1 :(得分:3)

你需要调用翻转功能。我也建议使用random.randint()

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;

        TextView textView = new TextView(this);
        textView.setText("Main Activity");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22f);
        textView.setLayoutParams(layoutParams);
        layout.addView(textView);

        Button button = new Button(this);
        button.setText("Start Activity 2");
        button.setLayoutParams(layoutParams);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Activity2.class);
                startActivity(intent);
                finish();
            }
        });
        layout.addView(button);
    }
}


public class Activity2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;

        TextView textView = new TextView(this);
        textView.setText("Activity 2");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22f);
        textView.setLayoutParams(layoutParams);
        layout.addView(textView);

        Button button = new Button(this);
        button.setText("Start Activity 3");
        button.setLayoutParams(layoutParams);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Activity2.this, Activity3.class);
                startActivity(intent);
                finish();
            }
        });
        layout.addView(button);
    }
}

public class Activity3 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;

        TextView textView = new TextView(this);
        textView.setText("Activity 3");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22f);
        textView.setLayoutParams(layoutParams);
        layout.addView(textView);
    }
}