-1

Update: I've added the parentheses and byte however the code still doesn't change the output.

I'm trying to write a python program that sees what the user is typing using getch() and then changes it (and prints the changed version) - so if the user types '1', 'one' would be printed for example.

Here's my code:

import msvcrt as m
character = m.getch
while True:
    if m.getch == b'1':
        print 'one'
        break

This just prints what the user types.

Please help me get the program to change the variable correctly.

That1Guy
  • 7,075
  • 4
  • 47
  • 59
Ollie
  • 21
  • 4
  • I can't test this (not on Windows) but it looks like you are assigning the **function** `m.getch` to `character`. Call it instead: `character = m.getch()` -- then what is its value? (You can always `print character` or be fancy and insert `import pdb;pdb.set_trace()` below it to see what the value of it is during execution.) – Two-Bit Alchemist Apr 06 '14 at 09:50

1 Answers1

0

As per my understanding of your question, I have created the code snippet below to fulfill your requirements

if the user types '1', 'one' would be printed

For demo I did some more stuff like if user presses 2 then 'Two' will be displayed and if the user presses any other key, it will be displayed by default.

Code:

import msvcrt as m
character = m.getch()
while True:
    if character == '1':
        print 'one'
        break
    elif character =='2':
        print 'Two'
        break
    else:
        print character
        break
anon582847382
  • 19,907
  • 5
  • 54
  • 57
ρss
  • 5,115
  • 8
  • 43
  • 73