Why doesn't the 'if' function work? python [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 6 years ago.
Improve this question
def guessinggame ():
d = input("Enter a random word: ")
e = input("How many letters does the word has?: ")
f = len(d)
if( "e" == len(d)):
print("You are right!")
guessinggame()
if I try to run this code, it will work, but it will skip the "if" function... Help please. thanks!

You are working only with strings and never converting anything to integers, and (more importantly) you are confusing a string with a reference name. For integers, use int() to cast:
e = int(input("How many letters does the word has?: "))
For the strings vs reference names, 'e' is just the letter "e." Omit the quotes and you will then be using a reference e.
if e == len(d):

e needs to be an int.
def guessinggame():
d = input("Enter a random word: ")
e = input("How many letters does the word has?: ")
f = len(d)
if int(e) == len(d):
print("You are right!")
guessinggame()

Related

Unable to add item to dictionary [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 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)

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)

Why aren't these two strings the same, dictionary and input? [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 5 years ago.
Improve this question
In the code:
skill = input("Which skill would you like to increase by 1 ?: ")
for x in abilities:
if x.lower() == skill.lower():
abilities[x] += 1
break
print("Sorry, I don't see that skill...")
and the dictionary being:
abilities = {
"STR" : 10,
"DEX" : 10,
"CON" : 10,
"INT" : 10,
"WIS" : 10,
"CHR" : 10 }
Then when for the input the string "STR" is put in, I get the response telling me the strings are not identical.
To my knowledge they are? Is there a really simple mistake here which I'm accidentally looking over, or is there a certain rule with this kind of thing?
for x in abilities:
if x.lower() == skill.lower():
abilities[x] += 1
break
print("Sorry, I don't see that skill...")
that will print the error message regardless of the loop outcome.
Just add a else to your for loop and it will work
for x in abilities:
if x.lower() == skill.lower():
abilities[x] += 1
break
else:
# called when for loop ended without hitting break or return
print("Sorry, I don't see that skill...")
however this is a very inefficient way of counting, you're not using the dictionary as it is, but just as a list of tuples, so linear search, highly inefficient
Use (without a loop):
skill = skill.upper() # so casing matches the keys
if skill in abilities:
abilities[x] += 1
You know that abilities' keys are all upper-case strings, so you should convert the input to uppercase too:
skill = input("Which skill would you like to increase by 1 ?: ").upper()
if skill in abilities:
abilities[x] += 1
else:
print("Sorry, I don't see that skill...")

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'

when i put 3 if statements prints all of them [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
The following uses variables I have not shown the initialization of, but you can make up your own. It does exactly what I want it to do but it prints all of the if's not just 1 one the if's.
m = input()
if m == 1:
print(l)
time.sleep(1)
else:
print(cost + f + baws)
boss_he = boss_he - 20
print("%" + str(boss_he) + " boss health left")
if m == 2:
time.sleep(1)
else:
print(cost + baws)
boss_he = boss_he - 25
print('%' + str(boss_he) + ' boss health left')
if m == 3:
time.sleep(1)
else:
print(h)
wiz_he = wiz_he + 15
print('%' + str(wiz_he) + " of you health left")
Whenever I run this code it will execute every else case, why?
If each else prints a 1, or a 1, or a 3 if I put that number, it will print 1 2 and 3 instead if just printing 1(if i asked it to print 1). Is it something wrong with my code?
Your input() will never evaluate to any of your if statmenet,s because you are taking input() as a str. Thus, all the elses are triggered. Instead, cast int: m = int(input()). Also, your logic is flawed, because if m is not 1, 2, or 3, all the else statements will be entered.
Why don't you use if,elif,elif and else instead of using if several times? And the program determines your input as string so that make it like m=int(input()) to convert your input to an integer

Categories

Resources