美文网首页
笨办法学python第4课

笨办法学python第4课

作者: 3ab9db71c244 | 来源:发表于2018-08-31 14:14 被阅读19次
cars = 100  #给变量赋值
space_in_a_car = 4.0  
drivers = 30
passengers = 90
cars_not_driven = cars - drivers #直接用变量做减法
cars_driven = drivers   
carpool_capacity = cars_driven * space_in_a_car  #乘法
average_passengers_per_car = passengers / cars_driven #除法

print(f"there are { cars } cars available.")  #打印字符串跟变量
print("there are only", drivers,"drivers available.")  
print("there will be", cars_not_driven,"empty cars today.")
print("we can transport",carpool_capacity, "people today.")
print("we have",passengers, "to carpool today.") #打印字符串
print("we need to put about", average_passengers_per_car,  "in each car.")


显示结果:
$ python3.6 ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3.0 in each car

练习题:
Study Drills
When I wrote this program the first time I had a mistake, and Python told me about it like this:
Traceback (most recent call last):
File "ex4.py", line 8, in <module>
average_passengers_per_car = car_pool_capacity / passenger
NameError: name 'car_pool_capacity' is not defined
Explain this error in your own words. Make sure you use line numbers and explain why.

因为不需要下划线,多了下划线,名字就变了



Here are more study drills:
1. I used 4.0 for space_in_a_car, but is that necessary? What happens if it’s just 4?

小数位后面的区别

2. Remember that 4.0 is a floating point number. It’s just a number with a decimal point, and you need 4.0 instead of just 4 so that it is floating point.

是的

3. Write comments above each of the variable assignments.
写了,相同的略过

4. Make sure you know what = is called (equals) and that its purpose is to give data (numbers,strings, etc.) names (cars_driven, passengers).

=号就是赋值的意思

5. Remember that _ is an underscore character.
下划线

6. Try running python3.6 from the Terminal as a calculator like you did before, and use variablenames to do your calculations. Popular variable names include i, x, and j.

常见问题:
1.What is the difference between = (single-equal) and == (double-equal)? 

The = (single-equal) assigns the value on the right to a variable on the left. The == (double-equal) tests whether two things
have the same value. You’ll learn about this in Exercise 27.

一个等号是赋值      两个等号是相同

2.Can we write x=100 instead of x = 100? You can, but it’s bad form. You should add space around operators like this so that it’s easier to read.

要加空格,不要太紧密

3.What do you mean by “read the file backward”? 

Very simple. Imagine you have a file with 16 lines of code in it. Start at line 16, and compare it to my file at line 16. Then do it again for 15, and so on until you’ve read the whole file backward.

倒过来阅读坚持,感觉会更有耐心,因为知道接近开始了,其实这样就是有进度条了。



4.Why did you use 4.0 for space_in_a_car? 

It is mostly so you can then find out what a floating point number is and ask this question. 





相关文章

网友评论

      本文标题:笨办法学python第4课

      本文链接:https://www.haomeiwen.com/subject/sfnkwftx.html