【问题标题】:py-3 | Trying to call a function based on an input doesnt work properlypy-3 |尝试根据输入调用函数无法正常工作
【发布时间】:2018-07-15 20:19:21
【问题描述】:

这是我的问题:/

所以我一直在从事这个项目,但我找不到任何问题的答案......为什么我要多次输入字母 C 或 c 才能调用 c() 函数,有时甚至不工作?我的脚本正确吗?

脚本:

def c():
    print ('do something')

def s():
    print ('do something else')

def main():    
    if input() == 'S' or input() == 's':
        print ('Please enter the number you want to start with:')
        s()

    elif input() == 'C' or input() == 'c':
        print ('Please enter the number you want to check:')
        c()

    else:
        print ('Please enter either S or C')
        main()

print ('You want to Start with or Check a number? Enter S or C.')

main()

脚本结果 1:

You want to Start with or Check a number? Input S or C.
c
c
c
c
do something

脚本结果 2:

You want to Start with or Check a number? Input S or C.
c
C
c
C
Please input either S or C
You want to Start with or Check a number? Input S or C.

编辑:我很抱歉帖子的质量很差,但这是我的第一篇!

【问题讨论】:

  • 欢迎来到 Stack Overflow!请edit您的帖子将源代码包含为格式化文本和您的问题的文本描述。屏幕截图是不够的。
  • input() == 'S' or input() == 's' 如果不是 S,则要求输入两次。input() == 'C' 如果不是 S/s,则要求第三次输入。 input() == 'c' 第四次询问是否不是 S/s/C。

标签: python python-3.x if-statement input


【解决方案1】:

您应该将输入设置为变量,并将该变量发送给函数。

喜欢:

x = input('please enter a selection')

然后定义你的函数

def s(x):
    print('you selected: ' + x)

【讨论】:

  • 非常感谢!设置 input() 等于一个变量解决了我的问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 2019-09-13
  • 2013-05-03
  • 1970-01-01
相关资源
最近更新 更多