If by "array" you actually mean a Python list, you can use
a = [0] * 10
or
a = [None] * 10
Answer from Sven Marnach on Stack OverflowIf by "array" you actually mean a Python list, you can use
a = [0] * 10
or
a = [None] * 10
You can't do exactly what you want in Python (if I read you correctly). You need to put values in for each element of the list (or as you called it, array).
But, try this:
a = [0 for x in range(N)] # N = size of list you want
a[i] = 5 # as long as i < N, you're okay
For lists of other types, use something besides 0. None is often a good choice as well.
Here, I took the liberty to tidy up the code.
def start(): # don't use the name input()
nextRound = ['Dad', 'Mom']
maleNames = ['son', 'James', 'Mick', 'Dad']
a = int(input('Enter a number please'))
if a == 1:
nextRound, maleNames = ErrorInput(1, nextRound, maleNames) # if you want nextRound, maleNames to be [] you have to assign it to them
print(nextRound, maleNames)
def ErrorInput(error, namelist, round):
error_codes = {1: 'ERROR: You have entered a number above what is impossible to score', 2: 'ERROR: You Have entered a score meaning both players win',3: 'ERROR: You have entered a score meaning neither of the two players win', 4: 'ERROR: You have entered a negative number, this is impossible'} # too many ifs makes one wonder if a dictionary is the way to go
print(error_codes.get(error, 'Unknown Error'))
return([], []) # return the empty lists.
start()
Not sure why you go to such lengths to just get [], [] but whatever floats your boat.
Take a look at the notes on the code to get a better feeling of why I made the changes.
As per the comment above, i don't follow your code. But to iteratively empty a list try:
while len(maleNames) !=0:
popped = maleNames.pop()
print('Popped value: ', popped, '\tNew list: ', maleNames)
There are multiple problems with your code.
The first one is that you iterate over the key-value pairs in the dictionary, and later try to use the values as keys here: if trs[k] == i: and here translated.append(trs[v]) These should be just k and v instead of trs[k] and trs[v].
The second problem is a bigger one: after fixing the previous, the code still gives wrong answer. The words are in random order. This is because you iterate over the dictionary items in the outer loop instead of the words themselves. This can easily be fixed by changing the order of the loops.
The third is that I think the function should return a string. Just return " ".join(translated) at the end.
And the fourth is that you actually don't use the dictionary as a dictionary. You use it as a list, but it's not how they are meant to be used. dicts are direct mappings of values, you don't need to iterate over all the entries all the time. Use the in and [] operators.
So here is how this code should look like:
def translate(a):
trs = {"merry":"god", "christmas":"jul", "and":"och", "happy":"gott", "new":"nytt", "year":"ar"}
translated = []
for i in a.split(" "):
if i in trs:
translated.append(trs[i])
return " ".join(translated)
print translate("merry christmas and happy new year")
# prints "god jul och gott nytt ar"
def translate(a):
trs = {"merry":"god", "christmas":"jul", "and":"och", "happy":"gott", "new":"nytt", "year":"ar"}
translated = []
for i in a.split(" "):
for k, v in trs.iteritems():
if i == k:
translated.append(trs[i])
return translated
print translate("merry christmas and happy new year")
# output:
# ['god', 'jul', 'och', 'gott', 'nytt', 'ar']