【发布时间】:2018-08-01 10:33:59
【问题描述】:
我是 python 新手。我正在使用 MacOS,我有 python 2.7.15。
我正在尝试从用户那里获取输入:
#these two rows do not execute at the same time, it's just an example :)
annee = raw_input("Saisissez une annee : ") #works if input is string, but not if it's int
annee = input("Saisissez une annee : ") #works if input is int, but not if it's string
try:
anne = int(annee)
# isBissextile(annee)
except:
print("Erreur lors de la saisie")
正如我的 cmets 中所说,input 和 raw_input 没有按预期工作,我希望能够获取 int 或 string 的输入,然后显示异常与否
【问题讨论】:
-
你期待什么字符串?解析用户输入有什么问题?
-
我假设你想在这里输入一个类似数字的数字,对吧?然后你可以使用 input() 并通过 int() 解析它。它适用于 123 或 '123'
-
@Sayse 我想避免输入。
-
@Roel 是的,我只想读取整数(例如,一年)