作为小白新手,选择使用koggle上的引导速成教材入门。
语言学习总是类似的,首先是各种运算的介绍。
和其他语言比较不同的有:
符号 | 含义 |
---|---|
** | 指数运算 |
// | 取整 |
接着介绍各种函数
主要使用help和round。
round(num,n)
对于n为负数的情况,和设想的差距较大eg.round(3999,-3)=4000.
对于n为负数的情况,和设想的差距更大
print(round(2.665, 2))
print(round(2.675, 2))
输出为:2.67,2.67
and this is not a bug!
接着介绍条件语言的语法以及函数的定义
def sign(x):
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0
重点注意 :,以及elif。
在定义函数后,if, elif, else后都需要**:**
在熟练之后甚至可以有这种写法:
print("Splitting", total_candies, "candy" if total_candies == 1 else "candies")
pass 一般用于占位置。
在 Python 中有时候会看到一个 def 函数:
def sample(n_samples):
pass
该处的 pass 便是占据一个位置,因为如果定义一个空函数程序会报错,当你没有想好函数的内容是可以用 pass 填充,使程序可以正常运行。
头疼的思考题:
In this problem we’ll be working with a simplified version of blackjack (aka twenty-one). In this version there is one player (who you’ll control) and a dealer. Play proceeds as follows:
The player is dealt two face-up cards. The dealer is dealt one face-up card.
The player may ask to be dealt another card (‘hit’) as many times as they wish. If the sum of their cards exceeds 21, they lose the round immediately.
The dealer then deals additional cards to himself until either:
the sum of the dealer’s cards exceeds 21, in which case the player wins the round
the sum of the dealer’s cards is greater than or equal to 17. If the player’s total is greater than the dealer’s, the player wins. Otherwise, the dealer wins (even in case of a tie).
When calculating the sum of cards, Jack, Queen, and King count for 10. Aces can count as 1 or 11 (when referring to a player’s “total” above, we mean the largest total that can be made without exceeding 21. So e.g. A+8 = 19, A+8+8 = 17)
For this problem, you’ll write a function representing the player’s decision-making strategy in this game.
毫无思路。。。