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

input是一个int,变量是一个字符串,如果字符串大于if语句中的int,我想要一个动作。我正在使用 python 3.8


name = 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")


婷婷同学_
浏览 100回答 3
3回答

慕的地8271018

将age字符串转换为 int,然后签入 if 语句。age = int(input('How old are you?'))

噜噜哒

您可以在获取用户输入时使用将age变量转换为 an 。intint()name = input ("What is your name?")print(name)# make the input an intage= 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")

心有法竹

您需要将输入转换为 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 operatorif 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")和平相处
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python