My input var isn't working

I can’t figure out why this isn’t working here is the entire source code

import random as rand
import time as ti
print('Welcome to the 3 doors game')
ti.sleep(1)
nordoor=rand.randrange(1,3)
choose = input('which do you pick 1-3:')
if nordoor == 1:
    no_prize = rand.randrange(1,2)
    if no_prize == 1:
        no_prize = "door2"
    else:
        no_prize = "door3"
elif nordoor == 2:
    no_prize = rand.randrange(1,2)
    if no_prize == 1:
        no_prize = "door1"
    else:
        no_prize = "door3"
elif nordoor == 3:
    no_prize = rand.randrange(1,2)
    if no_prize == 1:
        no_prize = "door1"
    else:
        no_prize = "door2"
ti.sleep(1)
print(no_prize+" has no prize")
ti.sleep(1)
choose2 = input("do you want to change:")
if choose2 == 'no':
    if choose == nordoor:
        print('you win')
    else: 
        print('you lose')
if choose2 == 'yes':
    if no_prize == "door1":
        if choose == 2:
            last_choose = 3
            if last_choose == nordoor:
                print('you win')
            else:
                print('you lose')
    elif no_prize == "door2":
        if choose == 1:
            last_choose = 3
            if last_choose == nordoor:
                print('you win')
            else:
                print('you lose')
    elif no_prize == "door3":
        if choose == 2:
            last_choose = 1
            if last_choose == nordoor:
                print('you win')
            else:
                print('you lose')
    elif no_prize == "door2":
        if choose == 3:
            last_choose = 1
            if last_choose == nordoor:
                print('you win')
            else:
                print('you lose')
    elif no_prize == "door1":
        if choose == 2:
            last_choose = 3
            if last_choose == nordoor:
                print('you win')
            else:
                print('you lose')
    elif no_prize == "door3":
        if choose == 1:
            last_choose = 2
            if last_choose == nordoor:
                print('you win')
            else:
                print('you lose')
    elif no_prize == "door1":
        if choose == 3:
            last_choose = 2
            if last_choose == nordoor:
                print('you win')
            else:
                print('you lose')

What exactly doesn’t work as expected? Describe what you do, what you expect, and what you observe.

I noticed that you asked for the following input

choose = input('which do you pick 1-3:')

but never make use of the variable choose. Note that the return value for input will be a string and that a string can never be equal to an integer value. Thus, for example, choose == 1 would always be false no matter what value the user provides.

Also, when you have if/elif clauses to deal with some return values of input, you should also include an else clause in case you get an unexpected answer.

I am talking about the yes or no var

Hello,

this is actually a recurring theme :wink:.

This:

choose = input('which do you pick 1-3:')

is assumed to take in a type int (integer) but it is actually taking in a type str (string). Then, when you compare it here (there are others but only highlighting two instances for brevity):

it never matches because choose is not a number (type int).

What you want instead is:

choose = int(input('which do you pick 1-3:'))  # append the `int` at the beginning

Note that the keyword input ALWAYS takes in a type str even if the user enters a number. Therefore, you have to typecast it. Be it a type int or a type float.

Hope this helps.

1 Like

That helped but for some reason if you but a yes in to choose2 = input("do you want to change:") it just crashes

Note that note_prize is a type int per this:

To mitigate this, you can try this:

print(str(no_prize) + " has no prize")

Now we’re typecasting from type int to type str since you cannot concatanate numbers with strings.

1 Like

works a lot better but sometimes it does crash

I did some testing, and it seems like it only crashes when you use door 2 or 3

Most of the code under if choose2 == 'yes': will never run.

If no_prize is equal to "door1", "door2", or "door3", one of the first three condition will be true, and the following elifs will be skipped.

The remaining conditions are duplicates of those first three, which have already been handled.

1 Like

so if I replace them with if it will work?

I did testing as well. It does not crash for me. Which IDE are you using? Is it that you are closing it prematurely (force halting it) and you get a KeyboardInterrupt exception? If this is what you are referring to, you can fix this by wrapping the script with:

try:
    # code here

except KeyboardInterrupt:
    pass

I am using Visual studio code which just uses python’s IDE to interpetrate (sorry my spelling is really bad)

thanks you solved it

You should think about your approach. It can be done more simply.

I know i am just trying to get a working version finished