使用户输入等于变量的值

发布于 2025-02-04 18:21:33 字数 764 浏览 2 评论 0原文

因此,我是Python的新手,目前正在制作基于文本的游戏。我想向玩家提供随机生成的代码件,然后要求他们输入正确的组合。我将这些数字存储在列表中,将它们拼凑而成,并将它们加入在一起,从四个数字的列表中随机顺序获得四位数的数字。由于数字是随机的,所以我不知道组合,所以我不能要求确切的四位数号码。我试图使用户输入等于我存储四位数号码的变量,但它不起作用。它要么无法识别正确的数字,要么说“ int”对象是无法callable的。有人可以帮忙吗?

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2025-02-11 18:21:33

您在静态条件下犯了一个错误。使用C2()您调用C2中存储的int-object(您的错误消息指出)。为了解决这个问题,您的if statement应该看起来像这样:

if(c2==compcode):
    …

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):
    …
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文