• ベストアンサー

Pythonで

Pythonのホームページにあった 長方形の計算のプログラムをメモ帳でつくったんですが.... # The area of a rectangle # Ingredients: width = input("What is the width") height = input("What is the height") # Instructions: area = width*height print area こんなかんじで でもこれだと起動して縦と横の値を入れて答えがでた瞬間にウインドウが閉じてしまいます。 どうすればすぐに閉じなくなるんですか??

  • haku-d
  • お礼率54% (157/290)
  • Python
  • 回答数2
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
  • sakusaker7
  • ベストアンサー率62% (800/1280)
回答No.1

WindowsでPythonを使っていて、スクリプトをダブルクリックして起動してたりします? であれば print area の行の次に input("Hit any key to QUIT") とでも入れとけば入力待ちで止まりますよ。 でも、IDLEとか使ったほうが良いのではないですか?

haku-d
質問者

お礼

ありがとうございます! 止まりました IDLE って対話型のですよね?? いつでも使えるプログラムをつくりたかったので プログラミングは超初心者なので正直どうしたら一番いいのかがよくわからないんです…

haku-d
質問者

補足

すいません、もうひとつ質問なんですが。。。 今pythonのサイトのインスタントハッキングを見てやってるんですが、 http://www.python.jp/Zope/intro/instant_hacking_jp # Area calculation program print "Welcome to the Area calculation program" print "---------------------------------------" print # Print out the menu: print "Please select a shape:" print "1 Rectangle" print "2 Circle" # Get the user's choice: shape = input("> ") # Calculate the area: if shape == 1: height = input("Please enter the height: ") width = input("Please enter the width: ") area = height*width print "The area is", area else: radius = input("Please enter the radius: ") area = 3.14*(radius**2) print "The area is", area これに次のエクササイズの正方形や三角形、多角形をいれるにはどうすればいいんですか? ちょっと自分の力では理解できなくて・・・

その他の回答 (1)

  • sakusaker7
  • ベストアンサー率62% (800/1280)
回答No.2

なるほどそういう事情ですか。 いちいちダブルクリックするのもどうかと思ったんですが いじりながらプログラムを作るというのであればちとIDLEは不向きなところがあるかもしれませんね。 さて、補足にあった追加の質問ですが > これに次のエクササイズの正方形や三角形、多角形をいれるにはどうすればいいんですか? まず選択肢を増やさないといけないのはわかりますか? # Print out the menu: print "Please select a shape:" print "1 Rectangle" print "2 Circle" これの続きを書いてやります。 次に、 # Calculate the area: if shape == 1: ... という部分がここでは shape が1だったら(つまりRectangle(長方形だったら) これをする。 そうでなかったら(2が選択されたら)これをするという形になっているので、 shapeが1の場合 … shapeが2の場合 … shapeが3の場合 … shapeが4の場合 … のように追加していけばよいです。 そのための情報が "上のプログラムを正方形の面積も計算できるように拡張するには、ユーザーが 一辺の長さを入力しなくてはなりません。もし2つ以上の選択肢がある場合は、 このように書きます。 if foo == 1: # Do something... elif foo == 2: # Do something else... elif foo == 3: # Do something completely different... else: # If all else fails..." http://www.python.jp/Zope/intro/instant_hacking_jp これです。 つまり、 if shape==1: 長方形の場合の計算 elif shape==2: 円の場合の計算 elif shape==3: 正方形の場合の計算 … と増やしていけばよいです。 では頑張って!

haku-d
質問者

お礼

説明ありがとうございます! 選択肢を増やすのはわかったんですが・・・ # Area calculation proguram print "Welcome to the Area calculation proguram" print"-----------------------------------------" print # Print out menu: print "Please select a shape:" print "1 Rectangel" print "2 Circre" print "3 Square" # Get the use's choice: shape = input(">") if shape ==1: height = input("Pleasa enter the height:") width = input("Please enter the width:") area = height*width print "The area is",area elif shape ==2: radius = input("Please enter the radius:") area = 3.14*(radius**2) print "The area is",area elif shape ==3: one side = input("Please enter the one side:") area = one side**2 print "The area is",area input("Hit and key quet") とりあえず選択肢と正方形の面積の出し方も入力して あとは if foo == 1: # Do something... elif foo == 2: # Do something else... elif foo == 3: # Do something completely different... else: # If all else fails... これを入れればいいんですか?? どこに入れればいいんですか??? わからないこと多すぎてすいません。。。

関連するQ&A

  • 問題が解けません(インスタンスメソッド)

    こんにちは。 課題をやっているのですが、問題が解けません。 問題内容です。 以下のクラス図で示したRectangleクラスを作成し、実行結果と同じに なるようにメソッドを作成する。 クラス図 Rectangle -width:ing -height:int ----------- Rectangle() Rectangle(w:int,h:int) getArea():int ------------- 問題文 getArea()メソッドでは面積の計算をする(高さ×幅) コンストラクタRectangle()ではwidthとheightに初期値0を設定。 main()メソッドでは標準入力より幅、高さを整数として受け取り コンストラクタの引数とする。 実行結果 >10 >20 インスタンスr1の面積は200 >123 >45 インスタンスr2の面積は5535 -------------------------- 途中経過 import java.io.*; class Rectangle{ public static int sum=0; private int width; private int height; public Rectangle(){ width=0; height=0; sum++; } public void Rectangle(int w, int h){ width=w; height=h; } public void getArea(){ int num; num=width*height; System.out.println("r" + sum + "インスタンスの面積は" + num); } } class Ex54{ public static void main(String args[]){ BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); String wid = input.readLine(); String hei = input.readLine(); Rectangle rectangle1 = new Rectangle(); rectangle1.Rectangle(wid, hei); } } 「現在の問題点」 UML図の通り記述できているか自信がありません。 rectangle1.Rectangle(wid, hei);というところでエラーが出てしまいます。 r1、r2を表示するときにsumを使っているのですがこのような記述でいいか自身がありません。 どなたかよろしくお願いします。

    • ベストアンサー
    • Java
  • pythonでのエラー

    Pythonで以下のコードを実行すると12行目に対して 「ValueError: cannot reshape array of size 12288 into shape (4096,1)」 と出るのですがどなたか原因わかるでしょうか? import cv2 import numpy as np img_in = cv2.imread("Parrots.bmp") cv2.imshow("input", img_in) # img_out = cv2.GaussianBlur(img_in, (3,3), 0) # cv2.imshow("Gaussian", img_out) height, width = 64, 64 img_out = cv2.resize(img_in, (height, width)) x = np.reshape(img_out, (width*height,1)) # 画像のベクトル化 H = np.zeros((width*height, width*height), np.float32) for i in range(width*height):     if i-width-1 >= 0 and i+width+1 <width*height:         H[i,i-width-1] = 1/16         H[i,i-width] = 2/16         H[i,i-width+1] = 1/16         H[i,i-1] = 2/16         H[i,i] = 4/16         H[i,i+1] = 2/16         H[i,i+width-1] = 1/16         H[i,i+width] = 2/16         H[i,i+width+1] = 1/16 y = np.matmul(H, x)       y = cv2.GaussianBlur(y, (3,3), 0) cv2.imshow("Gaussian", y) noise = np.random.normal(0, 1, y.shape) y = np.clip(y + noise, 0, 255).astype(np.uint8) gamma = 1e-3 x_estimate = np.linalg.solve(np.matmul(H.T, H) + gamma*np.identity(height*width), np.matmul(H.T, y)) cv2.imshow("x_estimate",x_estimate)

  • Javaの基礎のプログラム

    結城浩さんの本に載っていたプログラムです。 ですが、説明がわかりにくくていまいち処理がわからなかったので 質問させていただきました。 どうやら、X軸とY軸がありY軸は下に行けばいくほど数字が大きくなる。 X軸は右に行けばいくほど大きくなる。座標上にある、二つの長方形の重なりあう部分の座標を求めるプログラムのようです。(イメージとしては、下に添付してある画像のような感じになるようです。) 質問としては一つです。 プログラム中のこの部分なのですが、おそらく「長方形の座標を示している」行であると思います。 a = new Rectangle(0, 0, 20, 10); b = new Rectangle(5, 5, 20, 10); c = new Rectangle(20, 10, 20, 10); d = new Rectangle(-10, -20, 100, 200); e = new Rectangle(21, 11, 20, 10); その後の処理としては、aとb→c→d→eというようにそれぞれ比較していっているのもなんとなくわかります。 ですが、しっかり解説されていなかったので、この4つの数字がどのように座標を示しているのかよくわかりませんでした。 よろしくお願いします。 class Rectangle { final int INITIAL_WIDTH = 10; final int INITIAL_HEIGHT = 20; int width; int height; int x; int y; Rectangle() { width = INITIAL_WIDTH; height = INITIAL_HEIGHT; x = 0; y = 0; } Rectangle(int width, int height) { this.width = width; this.height = height; this.x = 0; this.y = 0; } Rectangle(int x, int y, int width, int height) { this.width = width; this.height = height; this.x = x; this.y = y; } void setLocation(int x, int y) { this.x = x; this.y = y; } void setSize(int width, int height) { this.width = width; this.height = height; } public String toString() { return "[" + x + ", " + y + ", " + width + ", " + height + "]"; } Rectangle intersect(Rectangle r) { int sx = Math.max(this.x, r.x); int sy = Math.max(this.y, r.y); int ex = Math.min(this.x + this.width, r.x + r.width); int ey = Math.min(this.y + this.height, r.y + r.height); int newwidth = ex - sx; int newheight = ey - sy; if (newwidth > 0 && newheight > 0) { return new Rectangle(sx, sy, newwidth, newheight); } else { return null; } } public static void main(String[] args) { Rectangle a, b, c, d, e; a = new Rectangle(0, 0, 20, 10); b = new Rectangle(5, 5, 20, 10); c = new Rectangle(20, 10, 20, 10); d = new Rectangle(-10, -20, 100, 200); e = new Rectangle(21, 11, 20, 10); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + e); System.out.println("a と a の重なり = " + a.intersect(a)); System.out.println("a と b の重なり = " + a.intersect(b)); System.out.println("a と c の重なり = " + a.intersect(c)); System.out.println("a と d の重なり = " + a.intersect(d)); System.out.println("a と e の重なり = " + a.intersect(e)); } }

    • ベストアンサー
    • Java
  • show()について

    class Rectangle { public static void main(String args[]) { double base=6; double height=5; double area; area=calc(base, height); System.out.println("area="+area); void show(); } public static double calc( double base, double height){ double area; area=(base*height); return area; } } このように四角形の面積を求めるプログラムをつくったんですが、 これにshowメソッドを加えて縦よ横の長さを表示したいのですが どのようにすればいいのでしょうか?

  • python 絵をかいた後、inputを行いたい

    現在pythonの勉強を行っています。 Tkで線を引いた後、inputで指定した場所に絵をさらに追加したいのですが、 絵が描かれずに先にinputがでてしまってどうしたらよいか困っています。 下記のようなプログラムを今書いており(○×ゲームにしたい)、 どこを直したらよいかわかりません。 どうかご教授お願いします。 from Tkinter import * root = Tk() c0 = Canvas(root, width = 200, height = 200) c0.create_line(10, 70, 190, 70) c0.create_line(10, 130, 190, 130) c0.create_line(70, 10, 70, 190) c0.create_line(130, 10, 130, 190) c0.pack() for Y in range(9): a = input("どこに置く?: ") if Y%2==0: id = c0.create_oval(10+(60*a/3), 10+(60*a/3),70+(60*a/3) , 70+(60*a/3)) c0.pack() root.mainloop()

  • C言語

    入力した数の高さと幅の長方形を*で表示するプログラミング 縦4、横4のとき (例) **** **** **** **** この長方形を表示するプログラミングを 縁の太さ2 の「中が空白の長方形」になるようにするにはどのようなプログラミングにしたらいいのでしょうか? (例) 縦6 横7 ******* ******* **   ** **   ** ******* ******* 普通に長方形を表示するのはわかったのですが、中を空白にする方法がわかりません。 たぶんif文を使って空白と*を書き分けるのですよね? どのようなif文の条件にするのかが分かりません、どなたか教えてくださいお願いします。 長方形を表示する途中までやったのプログラミング #include <stdio.h> int main (void) {   int i, j, width, height;   scanf("%d %d", &width, &height);   for(j = 1; j <= height; j ++ ) {    for(i = 1; i <= width; i ++ ) {      printf("*");   }    printf("\n");  } return(0); }

  • [css]縦または横の長さを指定したい

    htmlとcssで、 任意の形の長方形のimgを、大きさの決まった正方形の中に最大限大きく表示したいです。 つまり、縦か横かの長い方のwidth(height)を一定の長さにしたいのですが、 そのような指定方法はあるでしょうか? よろしくお願いいたします。

    • ベストアンサー
    • HTML
  • テーブルの枠線とテキストフィールドの縦、横の隙間をなくしたい

    テーブルの枠線とテキストフィールドの縦、横の隙間をなくしたい テーブルの枠線とテキストフィールの枠を隙間なくピッチリとくっつけたい です。コードを掲示しますのでご存知の方はどなたか修正願います <body> <table width="900" border="1" style="height:25px;"> <tr> <td rowspan="2" width="400">&nbsp;</td> <td rowspan="2" width="50">&nbsp;</td> <td rowspan="2" width="100"><input type="text"readonly="readonly"value="サンプルシステム(株)" style="height:25px;"/>&nbsp;</td> <td rowspan="2" width="50">&nbsp;</td> <td rowspan="2" width="100"><input type="text"readonly="readonly" value="システム管理者"style="height:25px;"/>&nbsp;</td> <td width="60"style="height:12px;">実施日</td> <td width="140"><input type="text" readonly="readonly" width="140"value="2010/07/01" style="height:12px;"/></td> <tr> <td height="19" style="height:12px;">CODE</td> <td><input type="text" readonly="readonly" width="140" value="TTSORD50P" style="height:12px;"/></td> </tr> </table> </body>

    • ベストアンサー
    • HTML
  • テーブルの中にiframe

    テーブルの中にiframeをしているのですが どうしてもボタンが右側に少しはみ出ますし iframeは縦400pxを超えるページを表示すると 縦スクロールバーが表示されますが それも右側にはみ出ます。 どうにか800pxの中に収まらないものでしょうか? また、ボタンのボーダを下記ソースでは消していますが デフォルトで表示される立体感のあるボタンのまま 800pxの中に収めたいです。 HTML5、CSS2.1です。 ご相談お願いします。 <table> <tr style="width: 800px; height: 50px"> <td><input type="button" value="1"/></td> <td><input type="button" value="2"/></td> <td><input type="button" value="3"/></td> <td><input type="button" value="4"/></td> <td><input type="button" value="5"/></td> </tr> <tr style="width: 800px; height: 400px"> <td colspan="5" style="width: 800px; height: 400px"> <iframe src="hoge.htm"></iframe> </td> </tr> <tr style="width: 800px; height: 50px"> <td colspan="5" style="width: 800px; height: 50px"> <p>hoge</p> </td> </tr> </table> ------------------------------------------------------------ table { padding: 0px; margin: 0px; border-style: none; border-width: 0px; } tr td { padding: 0px; margin: 0px; border-style: none; border-width: 0px; } input { border-style: none; border-width: 0px; padding: 0px; margin: 0px; color: Red; width: 160px; height: 50px; font-size: 20px; font-family: HGS明朝B; background-color: transparent; } iframe { padding: 0px; margin: 0px; border-style: none; border-width: 0px; background-color: transparent; width: 800px; height: 400px; }

    • ベストアンサー
    • HTML
  • どうプログラミングすればよいか教えてください。

    HTMLは <body> <div id="wrapper"> <div id="header"></div> <div id="left"></div> <div id="middle"></div> <div id="right"></div> <div id="footer"></div> </div> </body> スタイルシートは <style type="text/css"> * { padding: 0px; margin: 0px; } #wrapper { width: 900px; margin-right: auto; margin-left: auto; } #header { width: 100%; height: 200px; background: red; } #left { width: 20%; height: 400px; background: blue; float: left; } #middle { width: 60%; height: 400px; background: orange; float: left; left: 20%; } #right { width: 20%; height: 400px; background: yellow; float: left; left: 80%; } #footer { width: 100%; height: 100px; background: green; clear: both; } </style> 上記のプログラムだとmiddleエリアに文字を沢山記入していくとエリアの縦の長さが 固定されていてエリア自体の範囲は延長されず文字だけが増えていきます。 文字の量に応じてleftエリア、middleエリア、rightエリアの 縦の長さが延長されるようになるにはどうプログラムすればいいでしょうか。 要するに オレンジのエリアに文字を記入すると掲載画像のように文字だけが表示されて 青、オレンジ、黄色のエリアは固定されたままです。 これを、どうすれば文字の量に応じて青、オレンジ、黄色のエリアが延長されて 表示されるか教えてください。

    • ベストアンサー
    • CSS

専門家に質問してみよう