3 回答

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

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")
和平相处
添加回答
举报