1.变量及类型
变量:编程中最基本的存储单位
python中的变量有6种
1.数值型: int(整数) float(浮点型) complex(复数)
2.布尔型: True False
3.字符串 string
4.列表 list
5.元组 tuple
6.字典 dictionary
需要注意一点是在python3中,用一个int 类型的数据对另一个int数据做除法的时候的到的数据是一个浮点型,在循环的时候需要注意这一点,不然程序会报错,如:
def reduceNum(n):
print(f’{n}=’,end=’ ‘)
if not isinstance(n, int) or n <= 0:
print(‘请输入一个正确的数字!’)
exit(0)
elif n in [1]:
print(f’{n}’)
while n not in [1]:
for index in range(2, n + 1):
if n % index == 0:
n /= index
if n == 1:
print(index)
else:
print(f’{index}*’, end=’’)
break
reduceNum(8)
在执行这一段代码的时候会出现:
Traceback (most recent call last):
File “E:/Project of python/newbie/1_10.py”, line 19, in
reduceNum(8)
File “E:/Project of python/newbie/1_10.py”, line 9, in reduceNum
for index in range(2, n + 1):
TypeError: ‘float’ object cannot be interpreted as an integer
这个时候我们会发现是因为n /= index这个代码算出的数据类型是浮点型,只需要将代码改为:
ef reduceNum(n):
print(f’{n}=’,end=’ ‘)
if not isinstance(n, int) or n <= 0:
print(‘请输入一个正确的数字!’)
exit(0)
elif n in [1]:
print(f’{n}’)
while n not in [1]:
for index in range(2, n + 1):
if n % index == 0:
n //= index
if n == 1:
print(index)
else:
print(f’{index}*’, end=’’)
break
reduceNum(8)
这样就不会