input vs. raw_input: Python Interactive Shell Application?

前端 未结 2 1634

I was working off of the answer in this question: Python Interactive Shell Type Application

My code looks like this

def main():
  while True:
    s =         


        
相关标签:
2条回答
  • 2021-01-19 11:50

    You're running it under Python 2.x, where input() actually evaluates what you type as a Python expression. Thus, it's looking for a variable named hello, and, since you haven't defined one, it throws the error. Either use Python 3.x, or use raw_input().

    From the parentheses in your print I assume you intended to run it under Python 3.x.

    0 讨论(0)
  • 2021-01-19 12:00
    if s == 'hello':
      print('hi')
    
    elif s == 'exit':
      break
    
    else:
      print('Undefined input')
    

    This should take care of undefined user input.

    0 讨论(0)
提交回复
热议问题