为了账号安全,请及时绑定邮箱和手机立即绑定

输入是一个int,变量是一个字符串,如果字符串大于if语句中的int

输入是一个int,变量是一个字符串,如果字符串大于if语句中的int

婷婷同学_ 2022-07-12 16:14:08
input是一个int,变量是一个字符串,如果字符串大于if语句中的int,我想要一个动作。我正在使用 python 3.8name = input ("What is your name?")print(name)# print ("Yes or No")age= input ("how old are you?")if age >= 50:    print (name, "You are looking good for your age!")else:    print(name, "You are getting old.")print("Peace Out")
查看完整描述

3 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

age字符串转换为 int,然后签入 if 语句。

age = int(input('How old are you?'))


查看完整回答
反对 回复 2022-07-12
?
噜噜哒

TA贡献1784条经验 获得超7个赞

您可以在获取用户输入时使用将age变量转换为 an 。intint()


name = input ("What is your name?")


print(name)


# make the input an int

age= int(input("how old are you?"))


if age >= 50:

    print (name, "You are looking good for your age!")

else:

    print(name, "You are getting old.")


print("Peace Out")


查看完整回答
反对 回复 2022-07-12
?
心有法竹

TA贡献1866条经验 获得超5个赞

您需要将输入转换为 int。输入将其作为字符串接收,然后您需要将其转换为 int 以便能够将苹果与苹果进行比较


age = int(input("How old are you?"))

在 python 3.8 中,您还可以使用海象运算符 (:= )。您的代码将如下所示:


# Assign the value from input and print in 1 line thanks to :=    

print(name := input("What is your name?"))


# Make age an int and assign the value in your if statement with the walrus operator

if age := int(input("how old are you?")) >= 50:

    print (name, " You are looking good for your age!")

else:

    print(name, " You are getting old.")


print("Peace Out")

和平相处


查看完整回答
反对 回复 2022-07-12
  • 3 回答
  • 0 关注
  • 99 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号