【问题标题】:Input Problems Python 3.9 [duplicate]输入问题 Python 3.9 [重复]
【发布时间】:2020-10-07 16:46:26
【问题描述】:

我对编程非常陌生,并且试图弄乱输入。几周前我写了一个简单的生日计算器:

    name = " # name"
bday = # year of birth
current_year = 2020

age = current_year - bday

print(name.title() + " will turn:")
print(age)
print("this year")

我决定通过使用实际输入来改进这个概念,并开始在 Sublime 中搞乱。它允许我输入名称,但是当我按下回车键时,什么也没有发生。

这是程序的输入版本:

current_year = 2020
name = str(input("what is the name: "))
bday = str(input("what year were they born in: "))

age = current_year - bday

print(name.title() + " will turn:")
print(age)
print("this year")

【问题讨论】:

  • 不要在输入函数中使用str函数
  • 还是不行:\
  • current_year 是一个整数,而bday 是一个字符串——因此减法是行不通的

标签: python python-3.x input sublimetext3


【解决方案1】:

当您要求用户输入生日时再次查看您的代码,然后将其转换为 str。所以除了 str() 使用 int()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2021-02-01
    • 2011-11-22
    • 2019-03-29
    • 2016-05-11
    相关资源
    最近更新 更多