sách gpt4 ai đã đi

Python输入函数,打印

In lại 作者:行者123 更新时间:2023-12-04 13:26:30 29 4
mua khóa gpt4 Nike

我正在尝试创建一个简单的函数,其中 python 将根据您的年份输入计算年龄。我已经尝试了几种方法,但我没有运气 atm。
附:对不起,我是新手。

ame = input(" Enter your name: ")

age = input(" When were you born?: ")

print("Hello " + name + "! You are " + input (2021 - age)

1 Câu trả lời

import datetime

# get the year so it works all the time.
year = datetime.datetime.today().year
name = input(" Enter your name: ")
birth_year = input(" When were you born (year) ?: ")

# calclute the age
age = year - int(birth_year)

print("Hello " + name + "! You are ", age)
还有其他打印方式可能看起来更干净:
print(f'Hello {name}! You are {age}')
hoặc
print("Hello {0}! You are {1}".format(name, age))
使其成为函数:
import datetime

def age_cal(birth_year):
# get the year so it works all the time.
year = datetime.datetime.today().year
return year - int(birth_year)

if __name__ == "__main__":
name = input(" Enter your name: ")
birth_year = input(" When were you born (year) ?: ")
age = age_cal(birth_year)

print("Hello {0}! You are {1}".format(name, age))

关于Python输入函数,打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68183114/

29 4 0
Bài viết được đề xuất: python - 使用 conda 和 pip 时,如何在 Python 中固定版本化依赖项?
Bài viết được đề xuất: aurelia - 在 Aurelia View 中使用文字 JavaScript 值
Bài viết được đề xuất: Qt Qbrush 问题
Bài viết được đề xuất: vaadin14 - 如何更改 Vaadin-Select 悬停颜色
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com