【问题标题】:Mini-game with Python 3.2 does not work使用 Python 3.2 的迷你游戏不起作用
【发布时间】:2015-01-03 13:24:40
【问题描述】:
from random import randrange
def init_config(m, n):
    print("""Crée une configuration pour la configuration initiale""")
    config = [[0 for a in range(n)] for b in range(m)]
    for k in range(m*n): a, b = k//n, k%n
    config[a][b]=k+1
    return config
def disp(config, m, n):
    print("""Affiche un damier d'une configuration existante avec le format voulu""")
    s=t=" +%s\n" % ("---+"*n)
    for k in range(n):"%s %s" % (" "if k==0 else "",chr(65+k if k<26 else 71+k)),
    for k in range(m*n): i, j=k/n, k%n
    s +="%s%s|%03d%s"%(k/n+1 if k%n==0 else ""," "if k%n==0 and k/n<9 else "",config[a][b],"|\n" + t if b == n-1 else "")
    return s
def set_treasure (config):
    import random
    a=random.randrange(0,lin)
    b=random.randrange(0,col)
    treasure=config[a][b]=0
def main():
    print("Entrer les dimensions du damier")
lin=int(input('nombre de lignes : '))
if type (lin)!=int :
    print(""" ***ERREUR !***\n le nombre de lignes doit etre un nombre entier""")
    lin=int(input('number of lines : '))
elif lin>26:
    print(""" ***ERREUR !***\n le nombre de lignes ne doit pas excéder 26 !""")
    lin=int(input('nombre de ligne : '))
col=int(input('nombre de colonne: '))
if type (col)!=int :
    print(""" ***ERREUR !***\n le nombre de colonnes doit etre un nombre entier""")
    col=int(input('nombre de colonne: '))
elif col>38:
    print(""" ***ERREUR !***\n le nombre de colonnes ne doit pas excéder 38 !""")
    col=int(input('nombre de colonne: '))
n_treasure=int(input('Combien de trésor voulez vous mettre dans le jeux: '))
if type (n_treasure)!=int :
    print(""" ***ERREUR !***\n le nombre de trésor doit etre un nombre entier""")
    n_treasure=int(input('nombre de trésor que vous avez demander dans le jeux: '))
config=init_config(lin,col)
for k in range (n_treasure):
    if set_treasure (config):
        board=disp(config, lin, col)
        print(board)
for a in range (lin):
        for b in range (col):
            if config[a][b]==0:
                print("Il y a un trésor dans", chr(65+b),a+1) 

大家好,我刚刚用python 3.2完成了这个小游戏,但问题是程序无法运行我没有找到问题,我有TypeError: Win32RawInput() takes at most 2 positional arguments (4 given)

【问题讨论】:

  • input("Il y a un trésor dans", chr(65+b),a+1) 您在此声明中接受用户的输入?
  • 我弄错了,我没有使用输入而是打印
  • 现在我有另一个问题 NameError: name 'n_treasure' is not defined ,我认为这是一个间距问题,你怎么看?维维克黑貂
  • @AbdéNour 你能把你关于n_treasure的错误的回溯包括进来吗?
  • @Tanveer Alam 消息文件名行位置追溯 C:\Users\ABD\programme cherche trésor.py 48 NameError: name 'n_treasure' is not defined

标签: python python-3.2


【解决方案1】:

由于您没有对程序实际应该做什么提供任何解释,而且我不会说法语,所以我认为问题在于:在最后一行

input("Il y a un trésor dans", chr(65+b),a+1) 

您尝试显示几种不同的类型作为输入问题。但从上下文来看,我认为你真正想做的是打印这些类型。只需键入:

print("Il y a un trésor dans", chr(65+b),a+1) 

也许倒数第五行也是如此。应该是

print(board)

【讨论】:

    【解决方案2】:
    if config[a][b]==0:
       input("Il y a un trésor dans", chr(65+b),a+1)
    

    因此,您在这里将四个参数传递给内置函数input,该函数最多接受一个参数。

    input(...)
        input([prompt]) -> string
    
        Read a string from standard input.  The trailing newline is stripped.
        If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
        On Unix, GNU readline is used if enabled.  The prompt string, if given,
        is printed without a trailing newline before reading.
    

    【讨论】:

    • 谢谢你,我犯了一个愚蠢的错误,我不得不把“打印”而不是“输入”输入最多接受一个参数你是对的!谢谢 :)
    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2017-02-02
    • 1970-01-01
    • 2016-09-27
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多