練習 - 建置簡單的計算機
在此練習中,您會了解如何建置計算機。 計算機會接受輸入,並在畫面上顯示結果。 啟用沙箱之後,請重新整理頁面以重新啟動 Cloud Shell主控台。
在 Cloud Shell 主控台中輸入
python
,以啟動 REPL:python
您應該會看到如下所示的輸出:
Python 3.9.14 (main, Oct 29 2022, 22:18:10) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
將下列程式碼複製並貼入 REPL 中:
first_number = int(input('Type the first number: ')) ;\ second_number = int(input('Type the second number: ')) ;\ print("The sum is: ", first_number + second_number)
您應該會看見下列輸出:
>>> first_number = int(input('Type the first number: ')) ;\ ... second_number = int(input('Type the second number: ')) ;\ ... print("The sum is: ", first_number + second_number) Type the first number:
前兩個陳述式結尾的
;\
字元會指出有許多行程式碼。 使用這些字元可讓您逐行輸入所有程式碼。 程式碼也會執行,如您所見,會要求您提供第一個輸入。輸入 2 並按下 ENTER 鍵,以完成第一個輸入。
您應該會看見下列輸出:
Type the first number: 2 Type the second number:
現在,輸入 3 並按下 ENTER 鍵,完成第二個輸入。
您應該會看見下列輸出:
Type the second number: 3 The sum is: 5 >>>
恭喜,您已建立計算機程式。