Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不';我的输入不会被追加到数组中吗?(Python)_Python - Fatal编程技术网

为什么不';我的输入不会被追加到数组中吗?(Python)

为什么不';我的输入不会被追加到数组中吗?(Python),python,Python,这是我的代码: example = "house" example = example.upper() guess = '' array = [] def guess_function(guess): guess = input("Enter a letter: ") guess = guess.upper() while ord(guess) < 64 or ord(guess) > 90: print("Invalid input, p

这是我的代码:

example = "house"
example = example.upper()
guess = ''
array = []

def guess_function(guess):
    guess = input("Enter a letter:  ")
    guess = guess.upper()
    while ord(guess) < 64 or ord(guess) > 90:
        print("Invalid input, please try again..")
        guess = input("Enter a letter: ")
        guess = guess.upper()
    return guess

def populate_array(array):
    while len(array) < 26: #I have this condition so I can have every letter    in the alphabet
        guess_function(guess)
        array.append(guess)
        print(array)
        populate_array(array)
print(populate_array(array))
example=“house”
example=example.upper()
猜测=“”
数组=[]
def guess_函数(guess):
猜测=输入(“输入字母:”)
guess=guess.upper()
当ord(猜测)<64或ord(猜测)>90时:
打印(“输入无效,请重试…”)
猜测=输入(“输入字母:”)
guess=guess.upper()
返回猜测
def填充_阵列(阵列):
而len(array)<26:#我有这个条件,所以我可以拥有字母表中的每个字母
guess_函数(guess)
array.append(猜测)
打印(数组)
填充_数组(数组)
打印(填充数组(数组))

我希望这段代码将我猜的每个字母都附加到空列表中。但出于某种原因,我得到了这样的结果:['',每次我输入一个新的输入,它只会添加一组额外的''。我的代码有什么问题?

您已在顶部将“guess”指定为全局变量,但如果您计划在函数中访问它,则需要添加“global guess”。目前,您使用它作为局部变量,并返回结果,但从不将结果分配给可附加到数组中的内容

guess = input("Enter a letter:  ")
此赋值不会将
input(…)
的输出赋值给全局变量
guess
,而是赋值给外部无法访问的参数
guess

更好的版本是

example = "house".upper()
all_guesses = [] # Not used by me, but you'll use it

def get_guess():
    guess = input("Enter a letter:  ")
    guess = guess.upper()
    while ord(guess) < 64 or ord(guess) > 90:
        print("Invalid input, please try again..")
        guess = input("Enter a letter: ")
        guess = guess.upper()
    return guess

def populate_array():
    array = []
    while len(array) < 26:
        guess = get_guess()
        array.append(guess)
    return array
print(populate_array())
example=“house”。upper()
我没用,但你会用的
def get_guess():
猜测=输入(“输入字母:”)
guess=guess.upper()
当ord(猜测)<64或ord(猜测)>90时:
打印(“输入无效,请重试…”)
猜测=输入(“输入字母:”)
guess=guess.upper()
返回猜测
def填充_数组():
数组=[]
而len(数组)<26:
猜
array.append(猜测)
返回数组
打印(填充数组())

如果你真的,绝对不需要全局状态,就应该避免它
guess
不必是全局的,因为它不是在
get\u guess

之外使用的。您将一个空字符串(
guess='
)传递给
guess\u函数
,而没有将
guess\u函数
返回的值赋给变量。然后将空字符串
guess
附加到数组中,而不是一个保存
guess\u函数返回值的变量,这就是数组中填充
'
的原因

我已将注释添加到清理后的代码工作版本中,描述了我的选择

# Initializing a "guess" variable in global scope is unnecessary, because it's unused.
array = []

# This function doesn't need any arguments, because it handles all logic inside.
def guess_function():
    guess = input("Enter a letter:  ")
    guess = guess.upper()
    while ord(guess) < 64 or ord(guess) > 90:
        print("Invalid input, please try again..")
        guess = input("Enter a letter: ")
        guess = guess.upper()
    return guess

def populate_array(array):
    while len(array) < 26:
        # Call the function and assign to a variable the result.
        guess = guess_function()
        array.append(guess)
        # Because of the while loop, no need to recursively call the self-same function.
        # Instead of printing the array each time, return the array.
        # This way, the calling function can determine how to display the array.
    return array


print(populate_array(array))
#在全局范围内初始化“guess”变量是不必要的,因为它是未使用的。
数组=[]
#此函数不需要任何参数,因为它处理内部的所有逻辑。
def guess_函数():
猜测=输入(“输入字母:”)
guess=guess.upper()
当ord(猜测)<64或ord(猜测)>90时:
打印(“输入无效,请重试…”)
猜测=输入(“输入字母:”)
guess=guess.upper()
返回猜测
def填充_阵列(阵列):
而len(数组)<26:
#调用函数并将结果赋给变量。
猜测=猜测函数()
array.append(猜测)
#由于使用while循环,因此无需递归调用自同一函数。
#返回数组,而不是每次打印数组。
#这样,调用函数可以确定如何显示数组。
返回数组
打印(填充数组(数组))

嘿,非常感谢。它工作得非常好。很长时间没有使用Python了,事实上它是我的第一语言,所以我仍然在习惯一些东西。谢谢你的帮助。