Home > Net >  Making a user input equal to the value of a variable
Making a user input equal to the value of a variable

Time:06-07

So I'm pretty new to python and currently making a text based game. I want to give random generated code pieces to the player, then asking them to input the correct combination. I stored the numbers in a list, scrambled them and joined them together to get a four digit number in a random order from a list of four numbers. Since the numbers are random, I don't know the combination, so I can't ask for the exact four digit number. I tried to make the user input equal to the variable in which I stored the four digit number, but it doesn't work. It either doesn't recognise the correct number or say that 'int' object is not callable. Can someone help with this?

coderan = random.sample(safecode, len(safecode))

def convert(coderan):
    num = [str(n) for n in coderan]
    code = int("".join(num))
    return code

compcode = (convert(coderan))

print(type(compcode))

print(compcode)

c2 = int(input())
time.sleep(1)
ans = 'incorrect'
while(ans=='incorrect'):
    if(c2()==compcode):
        time.sleep(1)
        print(SCENE6COMP)
        ans = 'correct'
    else:
        time.sleep(1)
        print("That's not the right combination. Try again! ")
        c2 = input()

CodePudding user response:

You made a mistake in your if-statements condition. With c2() you call the int-object stored in c2 (which your error message states). To fix this, your if-statement should look like this:

if(c2==compcode):
    …
  • Related