使用户输入等于变量的值
因此,我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在静态条件下犯了一个错误。使用
C2()
您调用C2中存储的int-object(您的错误消息指出)。为了解决这个问题,您的if statement应该看起来像这样: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: