Unable to add item to dictionary [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am making a small calculator program because i'm bored, and i ran into a issue where i can't add item named Test with value awnser and i am unsure why, please help
question = []
awnslog = {
'None': 0
}
while True:
Inp = int(input('>> '))
Inp2 = str(input('>> '))
Inp3 = int(input('>> '))
question.insert(0, Inp3)
question.insert(0, Inp2)
question.insert(0, Inp)
if '*' or 'x' in question:
print('*')
print(question[0]*question[2])
awnser = question[0]*[question[2]
awnslog['Test'] = awnser # <--- Issue
print(awnslog)

The reason for failure is probably the SyntaxError here:
awnser = question[0]*[question[2]
It should have been awnser = question[0]*question[2] (Note the [ before question[2]. Also insert is expensive operation as it shifts all the elements in a list by one place. Also you are doing nothing in awnslog as everytime you add new entry with Test the older entry will be overwritten, so I am not sure what's the intention of having awsnlog. Here is one of the approach;
awnslog = {
'None': 0
}
while True:
question = []
Inp = question.append(int(input('First Number: ')))
Inp2 = question.append(str(input('Operation: ')))
Inp3 = question.append(int(input('Second Nummber: ')))
if question[1] in ['*', 'x', 'X']:
print(question[0]*question[2])
awnser = question[0]*question[2]
awnslog['Test'] = awnser
print(awnslog)

Related

Why is my function returning True even though the result is False? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
def funOne(x):
w = x.replace('-', '').replace(' ', '').replace('.', '')
y = len(w)
n = w
s = 0
for i in range(0,len(n)):
s = s + int(n[i])
if y == 11 and s == 44: #if both are True
return True
else:
return False
funOne(input('Type a number: '))
if funOne:
print('Ok')
else:
print('Error')
The "if" at the end doesn't belong to the main code it was just to check the result.
Use "87522231086" for a real Ok.
You need to either capture the return value when you call funOne:
x = funOne(input('Type a number: '))
if x:
print('Ok')
else:
print('Error')
or test the return value immediately:
if funOne(input('Type a number: ')):
print('Ok')
else:
print('Error')
Right now, you are testing if the function value itself is true, which (like all functions) it is.
>>> bool(funOne)
True
Because you aren't actually storing the result of your call to funOne. It needs to be:
if funOne(input('Type a number: ')):
print('Ok')
else:
print('Error')

Why does my code give me "list object is not callable"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm new to python and I've looked up a little bit of info and i cant't find the problem with my code, please help.
Code:
array = []
print ('Enter values in array: ')
for i in range(0,5):
n = input("value: ")
array.append(n)
a = input("Enter search term: ")
for i in range(len(array)):
found = False
while found == False :
if a == array(i):
found = True
position = i
else :
found = False
print("Your search term is in position " + position)
Error: at if a == array(i) line it says
list object is not callable
While it's generally hard to help without knowing what the error is, it's obvious here:
array(i)
is function call syntax, but lists aren't callable – you'll want subscript syntax:
array[i]
You need not have to run a while loop again while travering the array
array = []
print ('Enter values in array: ')
for i in range(0,5):
n = input("value: ")
array.append(n)
a = input("Enter search term: ")
for i in range(len(array)):
if a == array[i]:
position = i
if position:
print("Your search term is in position " + str(position))
else:
print('Not Found')
Here try this.
Also List/Dict can be accesed as list[i] not list(i)

No output when trying to print [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to write a python code in which I input a number, which should be a multiple of 3, and calculate the sum of each digit's power of 3,
ig: input'12' => multiple of 3
should return 1^3 +2^3 = 9
I tried this script and didn't get anything when I run it, what's the problem
number= input('give a number: ')
TT=list(number)
if int(number)==0:
print('wrong number')
elif int(number)%3:
for x in (range(len(TT))-1):
aggregate=sum(int(TT[x])**3)
print(f'the aggregate of {number} is {aggregate}')
None of the conditions are true, so no value ever gets displayed. If you input 12, then 12 % 3 equals 0 (False).
Take a look at the python truth value testing
You should update this line:
elif int(number)%3 == 0:
Now, the whole code needs a revision to get the result that you want. Let me make some changes:
number= input('give a number: ')
aggregate = 0
if int(number)==0:
print('wrong number')
elif int(number) % 3 == 0:
for x in number:
aggregate += int(x) ** 3
print(f'the aggregate of {number} is {aggregate}')

Python 3.5.1 if else: invalid syntax? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
number = [1,3,5]
position = 0
while
position < len(number):
numbers = number[position]
if numbers % 2==0:
print('found even number',numbers)
break
position = position +1
else:
I got SyntaxError: invalid syntax, after i push enter after else:
Help me please
The indentation of your position = position + 1 statement is wrong.
It is at the same level as your if and else statements, so separates them.
Indent or move it and you'll be fine.
Also, you could simplify the code a bit by changing the while loop to a for loop as:
number = [1, 3, 5]
for num in number:
if num % 2 == 0:
print("Found even number", num)
else:
Hard to say exactly given the short snippet of code, but it looks like the while loop is unnecessary given what is there

If statement doesn't print desired value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
It should print out stuff stored in generator variable if raw input was 1, but its not doing that, its executing else statement even if I write 1.
from random import randint
print('1. Generate again.')
print('2. Close')
x = raw_input('Pick your selection 1 or 2: ')
if x == 1:
generator = (randint(1000000000000000,999000000000000009))
print generator
else:
print 'bye'
You're comparing a string (what was input) with an int. Try if x == '1' instead.
Turn's answers is a good one, but there is an alternative way to do this by using int(). The built-in function int() treats the string as an int.
from random import randint
print('1. Generate again.')
print('2. Close')
x = raw_input('Pick your selection 1 or 2: ')
if int(x) == 1:
generator = (randint(1000000000000000,999000000000000009))
print generator
else:
print 'bye'

Categories

Resources