2

I am working on a simple project using getch(). When I apply the variable 'door', it only sets to door1.

 if getch()==(' '):
  clicks+=1 
  money += door
  print("$ =", money, ((clicks-100)*-1), "clicks remaining", door, door1, door2, door3)

 if getch()==('1'):
  door=door1
  clicks +=1
  print("door 1 chosen,",((clicks-100)*-1), "clicks remaining", door, door1, door2, door3)

if getch()==('2'):
  door=door3
  clicks+=1
  print("door 2 chosen, -1 clicks",((clicks-100)*-1), "clicks remaining", door, door1, door2, door3)

if getch()==('3'):
  door=door3
  clicks+=1
  print("door 3 chosen, -1 click",((clicks-100)*-1), "clicks remaining", door, door1, door2, door3)

The computer is reading that I have selected a different door. It prints what I've asked it to print and changes the click count, but the value of 'door' continues to be equal to the value of 'door1'. I don't fully understand why.

Just as an aside, (I don't know if this is going to be relevant or not) the code I've posted is all under a while True loop.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
m11879
  • 23
  • 4
  • What is `getch()`? – Pranav Hosangadi Apr 25 '22 at 17:34
  • Note that you don't need to surround your string on the right side of your comparisons (or anything, really) in parentheses. `x == 'abcd'` is the same as `x == ('abcd')` – Pranav Hosangadi Apr 25 '22 at 17:35
  • I presume you meant to call getch() once, and then test the key pressed for space, 1, 2, 3. Right now you are calling getch() multiple times. Store the keypress e.g. `c = getch()` and then compare `c` with space, 1, 2, 3. – jarmod Apr 25 '22 at 17:36
  • Also, you probably meant to take _one_ input and then compare that against your options, right? In that case you would take the input one time like `inp = getch()` and then compare it using an `if..elif..elif..else` ladder – Pranav Hosangadi Apr 25 '22 at 17:36
  • Have you considered using match/case? – DarkKnight Apr 25 '22 at 17:42
  • Also note that you have *two* conditions doing `door=door3`, and nothing using `door2` at all. – jasonharper Apr 25 '22 at 17:53

2 Answers2

1

Try to change your code and structure to be :

from getch import getch

#Read input data/values

str=getch()

#Make your conditions that depends on Input value(str)

if str==(' '):
  clicks+=1 
  money += door

 elif str=('1'):
  door=door1
  clicks +=1

elif str==('2'):
  door=door3
  clicks+=1

elif str==('3'):
  door=door3
  clicks+=1
1

Anytime you find yourself repeating code like that, there is a better way. Consider, for example, this sequence, which (I believe) does what you were trying to do, but in a way that can be extended without duplicating a bunch of code:


doors = [50,50,9000]
clicks = 0

while clicks < 100:
    ch = getch()

    if ch == ' ':
        money += door
        print("$", money,end='')
    else:
        door=doors[int(ch)-1]
        print("door",ch,"chosen,", end='') 

    clicks+=1 
    print(100-clicks, "clicks remaining", door, door1, door2, door3)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30