【MIT 6.0001 课程笔记】Problem Set 0

Problem Set 0

基本目标

A Very Simple Program: Raising a number to a power and taking a logarithm

  • The goal of this programming exercise is to make sure your python and numpy installations are correct, to get you more comfortable with using Spyder, and to begin using simple elements of Python. Standard elements of a program include the ability to print out results (using the print operation), the ability to read input from a user at the console (for example using the input function), and the ability to store values in a variable, so that the program can access that value as needed.
  • 验证你的python和numpy安装是否正确
  • 熟悉Spyder的使用
  • 学习使用基本元素
  • 学习print和input
  • 学习存储变量

作业要求

Write a program that does the following in order:
x y = ? x^y=? xy=?

l o g 2 ( x ) = ? log_2(x)=? log2(x)=?

  1. Asks the user to enter a number “x”
  2. Asks the user to enter a number “y”
  3. Prints out number “x”, raised to the power “y”.
  4. Prints out the log (base 2) of “x”.
  • Use Spyder to create your program, and save your code in a file named ‘ps0.py’. An example of an interaction with your program is shown below. The words printed in blue are ones the computer should print, based on your commands, while the words in black are an example of a user’s input. The colors are simply here to help you distinguish the two components.

  • 用Spyder编写程序,蓝色为系统输出,黑色为用户输入

    image-20201125161638231

提示

  • To see how to use the print command, you may find it convenient to look at the input and output of the Python Wikibook. This will show you how to use print statements to print out values of variables.
  • 可通过python教程来了解print的用法
  • To see how to read input from a user’s console into the Python environment, you may find it convenient to look at the same section (see for example the input() function)
  • 搜索input函数了解用法
  • Reference the basic math section of the Python Wikibook to read more about using basic mathematical operators in Python
  • 计算符号也可以在Wikibook查到
  • To take the logarithm of a variable, import either of the numpy or pylab packages. You can then call either numpy.log2 or pylab.log2 to calculate the logarithm. See the Getting Started document on importing packages and the many Numpy examples online for more info. Googling the log2 function may take you here, which has some helpful info.
  • 为了计算log,需要import numpy包或pylab包,可用numpy.log2 或 pylab.log2 来计算
  • Remember that if you want to hold onto a value, you need to store it in a variable (i.e., give it a name to which you can refer when you want that value). You may find it convenient to look at the variables and strings section of the Python Wikibook. (As you read through, remember that in Python 3.x you should be using input() not raw_input()). Take a look at the “Combining Numbers and Strings” sub-section, because you will be working with numbers and strings in this problem and will have to convert between the two using the str() and int() functions.
  • 若要存储一个数,需要将其赋值给一个变量。注意,在输出时需要把int数转换为str数,善用str()和int()

参考代码

import numpy

x = int(input("Enter number x:"))
y = int(input("Enter number y:"))
str1 = "x**y = %d" %(x**y)
print(str1)
str2 = "log(x) = %f" %(numpy.log2(x))
print(str2)

参考资料

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值