Ask coding questions

← Back to all posts
Python
WilliamXing (45)

How can I make my program work so that if I say Enter C for Cake and CA for cookie, If they enter C, they don't have to hit return?

Answered by Coder100 (17027) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (17027)

You can't, remember that CA would be ambiguous from C as the first key is both C.

What you should do instead is:

from getch import getch
key = getch()
if key == 'C':
  print('Cake')
elif key == 'A':
  print('COOKIE')

As you can see, if the user typed in C, you would either have to get input again or else it will always become Cake.

RYANTADIPARTHI (6001)

Solution

try this code.

import getch

inp = getch.getch()
print("Enter C for Cake and CA for cookie")
if inp == "C":
    print("cake")
else:
    print("Cookie")

like that
That should work

IntellectualGuy (691)

@RYANTADIPARTHI That won't work because getch only takes one character, and CA is 2

robowolf (436)

Try import getch. Then do answer = getch.getch() (this will cause them not to see what they type) or answer = getch.getche()which won't. However this is only for a single character so change CA to something like B.

EpicGamer007 (1615)

Well, that would be difficult because both C and CA start with C. if the users enters C then you have them not hit return, then how would u know if the user meant to write C or CA?