【发布时间】:2019-06-26 21:24:25
【问题描述】:
我正在尝试编写一个要求输入数字然后打印一行的函数。它对单个数字完美无缺,但是一旦我使用两位数字,我的 if/elif 语句只能看到第一个数字,而不是两个数字一起。
我在 python3 中创建了一个示例函数并运行了一个测试。我尝试将 str 更改为 int 也只是 input() 并没有任何效果。
>>> def test():
... out = str(input('Choice: '))
... if out[0] == '1':
... print('Test1 Worked')
... elif out[0] == '2':
... print('Test2 Worked')
... elif out[0] == '10':
... print('Test10 Worked')
...
>>> test()
Choice: 1
Test1 Worked
>>> test()
Choice: 2
Test2 Worked
>>> test()
Choice: 10
Test1 Worked
在最后一次运行 test() 时,我的选择是 10,我希望输出是 Test 10 Worked,但它显示 Test1 Worked。
【问题讨论】:
标签: python-3.x input integer