- int()取整是只取整数,而不是四舍五入
What will be the value of x when the following statement is executed:
x = int(98.6)
-->>98
- 变量名命名不能以数字开头,只能是字母或下划线
Which of the following is a bad Python variable name?
Spam
SPAM23
_spam
23spam (×)
只能由字母或下划线开头,并只能由字母、数字、下划线(underscore)组成
不能由数字开头
- print(‘’, x)字符串和变量一起输出时候中间会自动有一个空格
name = input("Enter your name")
print("Hello", name)
-->>Hello Sarah
- 乘法运算忘记加*
#错误
pay = 40*r+(h-40)(r*1.5)
#正确
pay = 40*r+(h-40)*(r*1.5)
- time-and-a-half 表示按照正常工资的1.5倍支付
- .format()函数
print("Maximum is {}\nMinimum is {}".format(largest, smallest))
'''
-->>
Maximum is 10
Minimum is 2
'''
- .isdigit()函数
#该代码中没有使用try和except检查错误,在检验输入是否为数字的时候用到了isdigit()函数。
count = 0
total = 0
while True:
shuru = input('Enter a number: ')
if shuru.isdigit():
count = count + 1
total = total + int(shuru)
continue
if shuru == 'done':
break
else:
print("Invalid input")
print('Done!')
print("num: ", count)
print("sum: ", total