练习 - 生成计算器

已完成

本练习将介绍如何生成计算器。 计算器使用输入并在屏幕上显示结果。 激活沙盒后,刷新页面以重启 Cloud Shell 控制台。

  1. 通过在 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.
    >>>
    
  2. 将以下代码复制并粘贴到 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:
    

    前两个语句末尾的符号 ;\ 表示存在许多代码行。 使用这些字符可逐行输入所有代码。 代码也会运行,并且如你所见,它会要求你进行第一行输入。

  3. 键入 2 并按 Enter,完成第一行输入。

    应会看到以下输出:

    Type the first number: 2
    Type the second number:
    
  4. 现在,键入 3 并按 Enter,完成第二行输入。

    应会看到以下输出:

    Type the second number: 3 
    The sum is:  5
    >>> 
    

恭喜!你已创建计算器程序。