Python3学习过程中遇到的小问题

1. input()函数

input函数的返回结果是字符串String类型,如果要进行数字的相加减,需要进行类型转换。如下:
在不进行转换时会报错:

x=input('Please input first num:')
y=input('Please input second num:')
rst=(x)*(y)
print(x,'x',y,'=',rst)
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    rst=(x)*(y)
TypeError: can't multiply sequence by non-int of type 'str'

更正如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
x=input('Please input first num:')
y=input('Please input second num:')
rst=int(x)*int(y)
print(x,'x',y,'=',rst)

同样如果输入的是浮点类型:

height=float(input('Please input your height:'))
weight=float(input('Please input your weight:'))
BMI=weight/((height)*(height))
print(BMI)
if BMI<18.5:
    print('Too light')
elif BMI<=25:
    print('Normal')
elif BMI<=28:
    print('Too height')
elif BMI<=32:
    print('Fat')
else:
    print('Serious Fat')

continue的理解

continue是跳过本次循环体之后的代码。
因此如下代码会陷入死循环,并且只会打印到11,即100~12

a=100
while a>0:
    if a==11:
        continue
    print(a)
    a=a-1   

而如下代码同样会陷入死循环,会打印到11,然后一直打印11:

a=100
while a>0:
    print(a)
    if a==11:
        continue
    a=a-1 

而如下的代码,continue不起任何作用:

a=100
while a>0:
    a=a-1   
    print(a)
    if a==11:
        continue

因而正确的写法:
循环体在continue上面,输出结构在continue下面。

a=100
while a>0:
    a=a-1   
    if a==11:
        continue
    print(a)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值