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...")
Related
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 12 days ago.
Improve this question
I am trying to code a program that take user input of orders and converts it to a list that I can then produce a sum based on the number of each word in the list. However, my code keeps producing a a sum that is not correct for the provided list. For example, if I enter Water Nachos Water Cheeseburger the intended sum is 24, but my code is producing 39 as the answer. Why is this and what is a potential fix?
x = input("What are your orders?")
orders = list(x.split())
sum = 0
for i in orders:
if i == "Nachos":
sum+=6
if i == "Pizza":
sum+=6
if i == "Water":
sum+=4
if i == "Cheeseburger":
sum+=10
else:
sum+=5
print(sum)
I expected a sum of 24, but got a sum of 39.
The last else part is only for the if condition i == "Cheeseburger"
So `sum+=5 will gets executed for all the other conditions.
You can just use if else if
x = input("What are your orders?")
orders = list(x.split())
sum = 0
for i in orders:
if i == "Nachos":
sum+=6
elif i == "Pizza":
sum+=6
elif i == "Water":
sum+=4
elif i == "Cheeseburger":
sum+=10
else:
sum+=5
print(sum)
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()
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
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
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 8 years ago.
Improve this question
I'm having trouble trying to get this code to work. It keeps coming up with SyntaxError: invalid syntax
adult = int(input("No adults:")
child = int(input("No children:")
type = int(input("Well done or Rare:")
if adult < 0:
print("Enter number >=0)
elif child < 0:
print("Enter number >=0)
elif type != "W" or type != "R":
print("error")
If the user types in the correct number I want it to go to the next question in line. If user inputs incorrect data I want the error message and then the same question to be repeated.
Thanks!!
Try this:
adult = int(input("No adults:"))
child = int(input("No children:"))
type = int(input("Well done or Rare:"))
if adult < 0:
print("Enter number >=0")
elif child < 0:
print("Enter number >=0")
elif type != "W" or type != "R":
print("error")
Compare it with your code.
Your code includes: missing ), ", bad indentation.
Note: Using type as a variable will mask the built-in function "type" within the scope of the function or the block which variable is defined within. So while doing so does not raise a SyntaxError, it seems a bad programming experience.
You have some missing brackets:
int(input("No adults:")
^
int(input("No children:")
^
int(input("Well done or Rare:")
^ need another to close int()
Also, make sure your indentation is correct:
if adult < 0:
print("Enter number >=0)
elif child < 0:
print("Enter number >=0)
elif type != "W" or type != "R":
print("error")