For Loop in Python isn't working [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
inputs = []
iterations = int(input())
for x in inputs:
currentInput = input()
inputs.append(currentInput)
print(x)
That code isn't working. It is supposed to make more "currentInput" variables based on "iterations". Thank you soooo, much, as this has been bugging me.

Your code isn't working because it's not right.
Your for loop is going through each element in the list inputs. But inputs is an empty list; you haven't added anything to it so the for loop won't work. You must have meant
for x in range(iterations):
currentInput=input()
inputs.append(currentInput)
print(currentInput)

Related

How do I get all first elements of python dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I used dictionaries the first time and I can't figure out how to get all first elements of a dictionary. The picture shows an example of my problem. I want to get the brand names, not "brand0, brand1" etc.
thisdict = {
"brand0": ("Ford", "green_car"),
"brand1": ("Audi", "yellow_car"),
"brand2": ("Porsche", "red_car")
}
You can use several aproaches to this problem but the easiest is probably this
firstItems = [value[0] for value in thisdict.values()]
this works the same as
firstItems = []
for value in thisdict.values():
firstItems.append(value[0])

is there an python method for solve this list index error? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have error that say: the list index out of range ?
equal_score = []
for i, j in enumerate(new_gd):
if i > len(new_gd):
break
if new_gd[i]['score'] == new_gd[i+1]['score']:
equal_score.append(new_gd[i])
equal_score.append(new_gd[i+1])'
Since you refer to the index i+1 you should do if i+1 >= len(new_gd): break so you make sure i+1 exists.

Python Logical Error in Loop while reading Dictionary [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am new to python and OOPS.I am expecting my module add_book to increment if book is already present in dictionary. Please help me .Not sure why for loop is not working as expected.
https://github.com/amitsuneja/Bookstore/commit/4aefb378171ac326aacb35f355051bc0b057d3be
You should not append to the list while you are still iterating it. Also, your code will append the new item for each item already in the list that has a different name. Instead, you should use a for/else loop. Here, the else case will only be triggered if you do not break from the loop.
for recordlist in self.mybooksinventory:
if self.name == recordlist['name']:
recordlist['quantity'] += 1
break # break from the loop
else: # for/else, not if/else !
self.mybooksinventory.append({'name':self.name,'stuclass':self.stuclass,'subject':self.subject,'quantity':1})

add keys and values from input method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a task and I did almost of it, but I'm stuck in how i will add keys and values into dictionary from user I think that I have to use input method but I'm not sure .
This code is what i done it shows the max value and key but i add the dictionary i want something like this
x= {'omar':20,'nagy':5}
maxKey= max(x, key=x.get)
maxValue=max(x.values())
print maxKey,maxValue
but the user is the one who enter the key and value
you can do:
count=2
x= {}
while count:
name=input('name:')
value=int(input('value:'))
x[name]=value
count-=1
maxKey= max(x, key=x.get)
maxValue=max(x.values())
print maxKey,maxValue
which will result in:

Why won't this answer to project euler number 4 work properly? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
numbers = []
palindromes = []
for i in range (100,999):
for j in range(100,999):
number = i * j
test = str(number)
if test == test[::-1]:
palindromes.append(number)
print(palindromes[-1])
This code gives back palindromes. However it gives back 580085, which is far from 906609 which is correct. Is there something in the code that makes it quit early?
Your list does contain all the palindromes in that range, but not necessarily in a sorted order; the biggest one is somewhere in the middle. Use max to retrieve it.
print(max(palindromes))

Categories

Resources